Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1679 Accepted Submission(s): 591
题意:给你一个串,要匹配anniversary,字段数不得大于3;
题解:吐槽一下,为毛是大于等于11就ac,大于等于12就wa,错了N次。。。。。明明长度是11但是就应该到12
的啊。。。
思路:从当前开始向后匹配;匹配完成就往下深搜,当匹配段数大于3
的时候就结束当前深搜。。。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
#define T_T while(T--)
typedef long long LL;
const int INF=0x3f3f3f3f;
char s[110];
char a[20]="anniversary";
int ans,len;
void dfs(int p1,int p2,int num){
if(num>3)return;
if(p2>=11){
// printf("%d\n",num);
ans=1;return;
}
// printf("%d\n",len);
// if(p1>len)return;
int x,y;
for(int i=p1;i<len;i++){
x=i;y=p2;
while(s[x]==a[y])x++,y++;
// printf("%d",y);
if(x!=i)dfs(x,y,num+1);
else dfs(x+1,y,num+1);
}
}
int main(){
int T;
SI(T);
T_T{
ans=0;
scanf("%s",s);
len=strlen(s);
dfs(0,0,0);
if(ans)puts("YES");
else puts("NO");
}
return 0;
}
原文:http://www.cnblogs.com/handsomecui/p/5023620.html