c語言單調遞增最長子序列

描述 
求一個字符串的最長遞增子序列的長度
 如:dabdbf最長遞增子序列就是abdf,長度爲4
輸入
第一行一個整數0<n<20,表示有n個字符串要處理
 隨後的n行,每行有一個字符串,該字符串的長度不會超過10000
輸出
輸出字符串的最長遞增子序列的長度
樣例輸入
3
aaa
ababc
abklmncdefg
樣例輸出
1
3
7


#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
int main()
{
int n,k[10000],h[1000],g=0,j,t;
  char c;
  char m[100000];
  scanf("%d",&n);
memset(k,0,sizeof(k)); 
  while(n--)
  {
 int i=0,l=0;
 getchar();
     while((c=getchar())!='\n')
{
m[i++]=c;
}
          for(j=0;j<i-1;j++)
 {
 if(m[j]<m[j+1])
k[l]=k[l]+1;
else
l++;
 }
 l++;
 if(l==2)
 {
 if(k[0]<k[1])
 {
                 t=k[0];
    k[0]=k[1];
k[1]=t;
 }
 }
          if(l>2)
 {
               for(j=0;j<l-1;j++)
 for(i=0;i<l-j-1;i++)
 if(k[i]<k[i+1])
 {
                       t=k[i];
          k[i]=k[i+1];
      k[i+1]=t;
 }
 }
         
          h[g]=k[0];
 g++;
     }
  for(int i=0;i<g;i++)
  printf("%d\n",h[i]+1);
return 0;  
}

發佈了40 篇原創文章 · 獲贊 7 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章