hdu 5311 Hidden String(find,substr)
生活随笔
收集整理的這篇文章主要介紹了
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≤n2. 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 BestCoder 1st Anniversary ($)?題意:從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?
轉載于:https://www.cnblogs.com/UniqueColor/p/4799024.html
總結
以上是生活随笔為你收集整理的hdu 5311 Hidden String(find,substr)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈数据结构-平衡二叉树
- 下一篇: jdbc基础知识