HDU-5510 Bazinga(strstr找子串)

Bazinga

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 7141    Accepted Submission(s): 2188


 

Problem Description

Ladies and gentlemen, please sit up straight.
Don't tilt your head. I'm serious.


For n given strings S1,S2,⋯,Sn , labelled from 1 to n , you should find the largest i (1≤i≤n) such that there exists an integer j (1≤j<i) and Sj is not a substring of Si .

A substring of a string Si is another string that occurs in Si . For example, ``ruiz" is a substring of ``ruizhang", and ``rzhang" is not a substring of ``ruizhang".  

Input

The first line contains an integer t (1≤t≤50) which is the number of test cases.
For each test case, the first line is the positive integer n (1≤n≤500) and in the following n lines list are the strings S1,S2,⋯,Sn .
All strings are given in lower-case letters and strings are no longer than 2000 letters.

 

Output

For each test case, output the largest label you get. If it does not exist, output −1 .

 

Sample Input

 

4 5 ab abc zabc abcd zabcd 4 you lovinyou aboutlovinyou allaboutlovinyou 5 de def abcd abcde abcdef 3 a ba ccc

 

Sample Output

 

Case #1: 4 Case #2: -1 Case #3: 4 Case #4: 3

 

Source

2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

 

Recommend

wange2014

借鉴:https://blog.csdn.net/floraqiu/article/details/78512986

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
//strstr(str1,str2)判断str2是不是str1的子序列
using namespace std;
int main()
{
    int t;
    int i,j;
    int cas=0;
    char str[550][2200];
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        scanf("%s",str[i]);
        int ans=-2;
        for(i=n-1;i>=1;i--)
        {
           if(!strstr(str[i],str[i - 1]))     //因为这是最直接的,在最直接的过度
           {
                    ans = max(ans,i);
                    for(j=i+1;j<n;j++)
                    {
                        if(!strstr(str[j], str[i - 1]))
                        ans = max(ans,j);
                    }
            }
        }
        printf("Case #%d: %d\n", ++cas,ans+1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xigongdali/article/details/82821643