码迷,mamicode.com
首页 > 其他好文 > 详细

Hidden String(深搜)

时间:2015-12-06 15:57:27      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

Hidden String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1679    Accepted Submission(s): 591

Problem Description
Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a string s of length n. He wants to find three nonoverlapping substrings s[l1..r1], s[l2..r2], s[l3..r3] that:
1. 1l1r1<l2r2<l3r3n
2. The concatenation of s[l1..r1], s[l2..r2], s[l3..r3] is "anniversary".
 

 

Input
There are multiple test cases. The first line of input contains an integer T (1T100), indicating the number of test cases. For each test case:
There‘s a line containing a string s (1|s|100) consisting of lowercase English letters.
 

 

Output
For each test case, output "YES" (without the quotes) if Soda can find such thress substrings, otherwise output "NO" (without the quotes).
 

 

Sample Input
2 annivddfdersewwefary nniversarya
 

 

Sample Output
YES NO
 

题意:给你一个串,要匹配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;
}

  

 

Hidden String(深搜)

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/5023620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!