hdu 5311 Hidden String(dfs)

Hidden String

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


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

solution:

沒什麼可說的,直接看代碼~

#include<cstdio>
#include<cstring>
using namespace std;
char s[150], a[20] = { "anniversary" };
int len;
bool dfs(int d, int x, int y)
{
    if (y == 11)return 1;
    if (d > 3)return 0;
    for (int i = x; i < len; i++)
    {
        int tmp1 = i, tmp2 = y;
        if (s[i] == a[y])
        {
            while (tmp1 < len&&tmp2 < 11 && s[tmp1] == a[tmp2])
            {
                tmp1++; tmp2++;
            }
            if (dfs(d + 1, tmp1, tmp2))return 1;
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%s", s);
        len = strlen(s);
        if (dfs(1, 0, 0))printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章