hdu 5311 Hidden String(find,substr)

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. 1≤l1≤r1<l2≤r2<l3≤r3≤n

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 (1≤T≤100), 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

 

 

 

Source
 

 题意:从s串中找出3段连续的字串组成“anniversary”

 

复习了下find,substr的用法,老是忘记

s.substr(i,j)表示从s串的i位置开始,长度为j的子串。

s.find(p,i),p为字串,表示从s串的i位置开始,寻找有没有等于p的子串,如果有返回s的首地址,否则返回-1

 

这题枚举“anniversary”的3个子串,在给出的s串中寻找就可以了!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cmath>
 7 #include<stdlib.h>
 8 #include<map>
 9 using namespace std;
10 string s;
11 string goal="anniversary";
12 bool solve(){
13     for(int i=1;i<=9;i++){
14         int ans1=s.find(goal.substr(0,i),0);
15         if(ans1<0) continue;
16         for(int j=1;j+i<=10;j++){
17             int ans2=s.find(goal.substr(i,j),ans1+i);
18             if(ans2<0) continue;
19             int k=11-i-j;
20             int ans3=s.find(goal.substr(i+j,k),ans2+j);
21             if(ans3<0) continue;
22             return true;
23         }
24     }
25     return false;
26 }
27 int main()
28 {
29     int t;
30     scanf("%d",&t);
31     while(t--){
32         cin>>s;
33         if(solve()){
34             printf("YES\n");
35         }
36         else{
37             printf("NO\n");
38         }
39     }
40     return 0;
41 }
View Code

 

posted @ 2015-09-10 20:19  UniqueColor  阅读(198)  评论(0编辑  收藏  举报