HDU 5311 Hidden String (elegant brute force), hdu5311
Hidden String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission (s): 52 Accepted Submission (s): 25
Problem DescriptionToday 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 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
2annivddfdersewwefarynniversarya
Sample Output
YESNO
Source BestCoder 1st Anniversary ($)
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5311
Question: In a string, we cannot find three consecutive intervals and splice them into anniversary.
Question Analysis: brute force, enumerative length of each segment
#include <cstdio>#include <cstring>char s[200], con[] = "anniversary";int main(){ int T; scanf("%d", &T); while(T--) { scanf("%s", s); int len = strlen(s); bool flag = false; for(int i = 0; i <= 8; i++) { for(int j = i + 1; j <= 9; j++) { int k = 0; while(k < len && strncmp(con, s + k, i + 1) != 0) k ++; if(k == len) continue; k += i + 1; while(k < len && strncmp(con + i + 1, s + k, j - i) != 0) k ++; if(k == len) continue; k += j - i; while(k < len && strncmp(con + j + 1, s + k, 10 - j) != 0) k ++; if(k != len) { flag = true; break; } } } if(flag) puts("YES"); else puts("NO"); }}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.