HDU-5311

Hidden String

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


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
 
Source
 
/**
    题意:给你一个串,看是否存在“anniversary”,并且是分成三段的
    做法:暴力
**/
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <string.h>
#define maxn 1010 + 10
using namespace std;
char c[120] = {"anniversary"};
__int64 index = 0;
char ch[maxn];
int solve(int tt,int len)
{
    __int64 res = 0;
    __int64 mmax = 0;
    __int64 tmp = 0;
    for(int i=tt;i<len;i++)
    {
        if(ch[i] == c[index])
        {
            int j;
            res = 0;
            for(j=0;;j++)
            {
                if(ch[i+j] == c[index+j] && i+j<len)
                {
                    res++;
                }
                else 
                    break;
            }
            if(res > mmax)
            {
                mmax = res;
                tmp = i + mmax;
            }
        }
    }
    index += mmax;
    return tmp;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        index = 0;
        scanf("%s",ch);
        __int64 len = strlen(ch);
        __int64 tt = 0;
        tt = solve(0,len);
        if(strcmp(ch,c) == 0)
        {
            printf("YES
");
            continue;
        }
        if(tt == 0) 
        {
            printf("NO
");
            continue;
        }
        else if(index == 11)
        {
            printf("YES
");
            continue;
        }
        tt = solve(tt,len);
        if(tt == 0) 
        {
            printf("NO
");
            continue;
        }
        else if(index == 11)
        {
            printf("YES
");
            continue;
        }
        tt = solve(tt,len);
        if(tt == 0) 
        {
            printf("NO
");
            continue;
        }
        if(index == 11) printf("YES
");
        else printf("NO
");
    }
    return 0;
}
Recommend
原文地址:https://www.cnblogs.com/chenyang920/p/4677280.html