contest: Codeforces Round #300, problem: (A) Cutting Banner,
開始時題意理解錯了,以為是隨便刪,只要能不改變順序地把剩下的拼起來組成CODEFORCES就行,之後才知道只能切去一段
All that we can manage to do is to cut out some substring from the banner, i.e. several consecutive letters.
這裡的some是某一的意思,因為後面的substring沒有加s.......
if the beginning or the end of the original banner was cut out, only one part remains.
i.e. after a substring is cut, several first and last letters are left, it is allowed only to glue the last letters to the right of the first letters.
起初對這兩句話很不解....
題意很重要啊TAT
======================================================================================================================================
~~華麗的分割線~~
因為只能切去連續的一段,所以只可能有三種情況
1.CODEFORCES------
2.-----CODEFORCES
3.CODEFORCES的前半部分-----CODEFORCES的後半部分
method 1:15ms
#include<iostream>#include<cstring>#include<cstdio>#include<string>using namespace std;///char s[100+10];string s,str1,str2;int main(){ char des[11]="CODEFORCES"; while(cin>>s) { int len=s.length(); int flag=0; if(len<10) { printf("NO\n");continue; } ///長度不夠肯定不行 for(int i=0;i<=10;i++) ///這裡是10不是len { /// memcpy(str,s,i); str1=s.substr(0,i); /// memcpy(str+i,s+i+len-10,10-i); str2=s.substr(i+len-10,10-i); /// if(!strcmp(str,des)) if(str1+str2==des) {flag=1;break;} ///string還可以這樣加 哈哈 } if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
method 2:30ms
#include<iostream>#include<cstring>#include<cstdio>using namespace std;char s[100+10];int main(){ char des[11]="CODEFORCES",str[11]; ///printf("%s\n",des); str[10]='\0'; while(~scanf("%s",s)) { int len=strlen(s); /// int cnt=0,k=0; int flag=0; if(len<10) { printf("NO\n");continue; } for(int i=0;i<=10;i++) { memcpy(str,s,i); memcpy(str+i,s+i+len-10,10-i); ///強大的memcpy if(!strcmp(str,des)) {flag=1;break;} } if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
那麼問題來了,第一個用的string為什麼反而快了呢?難道是 substr() 比 memcpy () 快?還是 strcmp() 比較慢?