Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
帮忙看看!为什么我的递归lcs会超时??!!连例子都要等很久!!谢了#include"stdio.h" #include"string.h" char s[105][35],p[105][35],output_lcs[105][35]; int c[105][105]; int lcs(int i,int j) { if(!i||!j)return c[i][j]=0; else if(strcmp(s[i-1],p[j-1])==0) c[i][j]=lcs(i-1,j-1)+1; else { int t1=lcs(i,j-1); int t2=lcs(i-1,j); c[i][j]=t1>t2?t1:t2; } return c[i][j]; } void output(int i,int j,int k) { if(!i||!j)return; else if(c[i][j]==c[i-1][j]) output(i-1,j,k); //i--; else if(c[i][j]==c[i][j-1]) output(i,j-1,k); //j--; else { strcpy(output_lcs[k],s[i-1]); output(i-1,j-1,k-1); //i--; //j--; //k--; } } int main() { int i=0,j=0,b; while(scanf("%s",s[i++])!=EOF) { while(scanf("%s",s[i])&&strcmp(s[i],"#")!=0) i++; while(scanf("%s",p[j])&&strcmp(p[j],"#")!=0) j++; lcs(i,j);//调用lcs output(i,j,c[i][j]);//输出函数 for(b=1;b<=c[i][j];b++) { printf("%s",output_lcs[b]); if(b!=c[i][j])printf(" "); } printf("\n"); i=j=0; }return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator