Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 2513 | Accepted: 940 |
Description
Input
Output
Sample Input
cat dig dog fig fin fine fog log wine
分析:DP,最长上升子序列。
Code:
#include <algorithm> #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <vector> #include <queue> #include <cmath> #include <map> #include <set> #define eps 1e-7 #define LL long long #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; const int maxn=250005; int dp[maxn]; char str[maxn][20],tmp[20]; char tar[]={'z','a','z'}; void add(int id,int pos,char des){ strcpy(tmp,str[id]); tmp[pos]=des; while(str[id][pos]) tmp[pos+1]=str[id][pos],pos++; tmp[pos+1]='\0'; } void del(int id,int pos){ strcpy(tmp,str[id]); pos++; while(str[id][pos]) tmp[pos-1]=str[id][pos],pos++; tmp[pos-1]='\0'; } void change(int id,int pos,char des){ strcpy(tmp,str[id]); tmp[pos]=des; tmp[strlen(str[id])]='\0'; } void edit(int id,int oper,int pos,char des){ switch(oper){ case 0:add(id,pos,des);break; case 1:del(id,pos);break; case 2:change(id,pos,des);break; } } int bsearch(int l,int r){ while(l<=r){ int m=(l+r)>>1; int x=strcmp(str[m],tmp) ; if(x==0) return m; else if(x<0) l=m+1; else r=m-1; } return -1; } int main() { int n=0; while(scanf("%s",&str[n++])!=EOF) ; int ans=1; for(int i=0;i<n;i++){ dp[i]=1; for(int op=0;op<3;op++){ for(int j=0;str[i][j];j++){ for(char c='a';c<=tar[op];c++){ edit(i,op,j,c); int p=bsearch(0,i-1); if(p>=0&&dp[p]+1>dp[i]){ dp[i]=dp[p]+1; if(dp[i]>ans) ans=dp[i]; } } } } } printf("%d\n",ans); return 0; }