(字符串) Hidden String -- HDU -- 5311

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5311

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


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

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define  N 150

char s1[20] = "anniversary";
char ch[N];
char s[N][3][20];

void Init()
{
    int j, z, w=0;

    for(j=1; j<=9; j++)
    for(z=1; z<=10-j; z++)
    {
        strncpy(s[w][0], s1, j);
        strncpy(s[w][1], s1+j, z);
        strcpy(s[w++][2], s1+j+z);
    }
}

int main()
{

   int t;
   scanf("%d", &t);

   Init();

   while(t--)
   {
       char ch[N];
       int i, a, b, len1, len2, flag=0;

       memset(ch, 0, sizeof(ch));
       scanf("%s", ch);

       for(i=0; i<45; i++)
       {
           if(strstr(ch, s[i][0]))
           {
               a = strstr(ch, s[i][0])-ch;
               len1 = strlen(s[i][0]);
               a = a + len1;

               if(strstr(ch+a, s[i][1])) 
               {
                   b = strstr(ch+a, s[i][1])-(ch+a); ///就在这, 我在判断的时候还把字符串向后移了 a 位, 但在                   ///计算的时候并没有用,这是最大的失误,难怪自己思路对了但一直提交错误
                   len2 = strlen(s[i][1]);
                   b = a + b + len2;

                   if(strstr(ch+b, s[i][2]))
                   {
                       flag = 1;
                       break;
                   }
               }
           }
       }

       if(flag)  printf("YES
");
       else  printf("NO
");
   }

    return 0;
}
勿忘初心
原文地址:https://www.cnblogs.com/YY56/p/4749504.html