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 |
不明白为什么会超时。。(附代码)链接后的字符串不用记录吗?#include<iostream> #include<string> using namespace std; string ss;//记录连接后的结果 string str[21]; int flag[21]; int n,ans=100000; bool ok=false; int concat(string str)//求ss串合并str串后,ss串需要增加的长度 { int len1=ss.length(); int len2=str.length(); int i,j,t=0,k; if(ss.length()==0)return len2; if(ss.find(str)!=4294967295){return 0;} if(str.find(ss)!=4294967295){return 0;} for(i=0;i<len1;i++) { for(k=i,j=0;k<len1&&j<len2;k++) { if(ss[k]==str[j]){t++;j++;} else {t=0;j=0;break;} } if(t!=0) { break; } } t=len2-t; return t; } void dfs(int ii) { int i,j; if(ii==n+1) { if(ans>ss.length())ans=ss.length(); //cout<<ss<<endl;//这里输出发现搜索次数确实已经很少了,,还是TLE了!!! return; } for(i=0;i<n;i++) { if(flag[i]==1)continue; int hh=concat(str[i]);//增加的长度 if(ss.length()+hh>=ans)continue; flag[i]=1; if(hh!=0) { for(j=str[i].length()-hh;j<str[i].length();j++) ss+=str[i][j]; } dfs(ii+1); string::iterator it=ss.begin(); ss.erase(it+ss.length()-hh,it+ss.length()); flag[i]=0; } } int main() { int k; int ca; char aa[21]; scanf("%d",&ca); while(ca--) { memset(flag,0,sizeof(flag)); ans=100000; scanf("%d",&n); for(k=0;k<n;k++) {scanf("%s",aa); str[k]=aa;} dfs(1); printf("%d\n",ans); } return 0; } ,,难道链接后的字符串不用记录吗? Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator