zoukankan      html  css  js  c++  java
  • HDU5311——字符串——Hidden String

    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
     


    Recommend
    hujie   |   We have carefully selected several similar problems for you:  5315 5314 5313 5312 5310 
    /*
    先求出前缀和后缀匹配,注意anniversaxxrxxy这组数据。。被卡了一上午,,因为访问到第二个a的时候会让前缀变成1
    然后就for循环找中间匹配的串,保证这个串的第一个和最后一个与头尾相连
    比赛的时候狗血代码都过了- -。。。如果自己hack自己涨分的话......
    */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    char  s1[110];
    char s2[110] = "anniversary";
    int pre1[110], pre2[110];
    int T;
    int main()
    {
        scanf("%d", &T);
        while(T--){
            scanf("%s", s1);
            int len1 = strlen(s1);
            int len2 = strlen(s2);
            memset(pre1, 0, sizeof(pre1));
            memset(pre2, 0 ,sizeof(pre2));
            int cout = 0;
            for(int i = 0 ; i < len1; i++){
                if(s1[i] == 'a' && cout == 0){
                    pre1[i] = 1;
                    cout = 1;
                }
                else if(s1[i] == s2[cout]){
                    pre1[i] = pre1[i-1] + 1;
                    cout++;
                }
                else {
                    pre1[i] = 0;
                    cout= 0;
                }
            }
            //    for(int i = 0; i < len1; i++)
            //        printf("%d ",pre1[i]);
            cout = 0;
            for(int i = len1 - 1; i >= 0 ; i--){
                if(s1[i] == 'y'){
                    pre2[i] = 1;
                    cout = 1;
                }
                else if(s1[i] == s2[len2 - cout-1]){
                    pre2[i] = pre2[i+1] + 1;
                    cout++;
                }
                else {
                    pre2[i] = 0;
                    cout = 0;
                }
            }
            //  for(int i = 0 ; i < len1; i++)
            //      printf("%d ",pre2[i]);
    
            int flag = 0;
    
            cout = 0;
            for(int i = 0 ; i < len1 ; i++){
                for(int j = i + 1; j < len1; j++){
                    cout = 0;
                    for(int k = i + 1; k < j; k++){
                        if(s1[k] == s2[pre1[i]+cout]){
                            cout++;
                        }
                        else {
                            cout = 0;
                            if(s1[k] == s2[pre1[i]+cout])
                                cout++;
                        }
                        if(cout + pre1[i] + pre2[j] == len2 && s1[k] == s2[len2 - pre2[j] - 1]){
                            flag = 1;
                            break;
                        }
                    }
                    if(flag) break;
                }
                if(flag) break;
            }
            //   for(int i = 0 ; i < len1; i++)
            if(flag) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    mahout下的KMeans Clustering实现
    信号量、互斥体和自旋锁
    找出二叉树中两个节点的最低共同父节点
    C/C++中volatile关键字
    函数指针和指针函数
    java终止线程的通用机制
    我的一次重构实践
    新手浅谈Future
    线程中添加线程
    死锁
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4677243.html
Copyright © 2011-2022 走看看