HDU-5510 Bazinga

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

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

题意:

求i最大的且其前无子串的字符串,若无则输出-1。

我们运用了求子串函数strstr();   关于strstr();的介绍请见:http://www.cnblogs.com/Kiven5197/p/5869909.html

先找两个相邻 若找出不匹配的串再反向对比。

附AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 //strstr();
 5
 6 char s[510][2010];
 7
 8 int main(){
 9     int t,n;
10     scanf("%d",&t);//cin>>t;
11     int ans=1;
12     while(t--){
13         scanf("%d",&n);//cin>>n;
14         for(int i=1;i<=n;i++){
15             scanf("%s",s[i]);//cin>>s[i];
16         }
17         int res=-1;
18         for(int i=n;i>0;i--){
19             if(!strstr(s[i],s[i-1])){
20                 res=max(i,res);
21                 for(int j=i+1;j<=n;j++){
22                     if(!strstr(s[j],s[i-1])){
23                         res=max(j,res);
24                     }
25                 }
26             }
27         }
28         printf("Case #%d: %d\n",ans++,res);//cout<<"Case #"<<ans++<<": "<<res<<endl;
29     }
30     return 0;
31 }

这题第一发用的cin和coutT掉了..改成scanf printf就A了

时间: 2024-08-24 13:13:34

HDU-5510 Bazinga的相关文章

hdu 5510 Bazinga(字符串kmp)

Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2287    Accepted Submission(s): 713 Problem Description Ladies and gentlemen, please sit up straight.Don't tilt your head. I'm serious.For

Hdu 5510 Bazinga(KMP+尺取法)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5510 思路:设两个指针l.r,对于字符串a.b.c,若a为b的子串则下次比较时可直接比较b,c.枚举r,依次比较s[l]--s[r-1]是否为s[r]的子串,若s[i]为s[r]的子串,则l++.否则答案更新为r. #include<cstdio> #include<cstring> #include<iostream> #include<algorith

【HDU 5510 Bazinga】字符串

2015沈阳区域赛现场赛第2题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:给定一个由字符串组成的序列,一共n个元素,每个元素是一个不超过2000个字符的字符串.求"存在秩小于 i 且不是 i 的子串"的最大的 i (1<= i <= n). 数据范围:n [1, 500],T组输入,T [1, 50] 思路:从1到n-1枚举每个字符串str[i],判断是否有 j < i 使得str[j]不是str[i

hdu 5510 Bazinga (KMP+暴力标记)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 思路: 一开始直接用KMP莽了发,超时了,后面发现如果前面的字符串被后面的字符串包含,那么我们就不需要用前面的字符串去比较了,把他标记掉就好了. 实现代码: #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> using namespace std; char

HDU 5510 Bazinga(KMP)

Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4572    Accepted Submission(s): 1459 Problem Description Ladies and gentlemen, please sit up straight. Don't tilt your head. I'm serious.F

hdu 5510 Bazinga 字符串+哈希

Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2078    Accepted Submission(s): 642 Problem Description Ladies and gentlemen, please sit up straight.Don't tilt your head. I'm serious.For

HDU 5510 Bazinga (KMP)

题意:给定 n 个 字符串,让你找出最大的 r,使得存在一个 sl 不是sr的子串(l  < r). 析:KMP算法,不过直接暴力就别想了,肯定TLE,所以我们考虑一下,用两个指针 l, r,如果sl 不是 sr的字串,那么们就可以更新r,继续往后,直到找到最后. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string>

hdu 5510 Bazinga(暴力)

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

hdu 5510 Bazinga (kmp+dfs剪枝) 2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

废话: 这道题很是花了我一番功夫.首先,我不会kmp算法,还专门学了一下这个算法.其次,即使会用kmp,但是如果暴力枚举的话,还是毫无疑问会爆掉.因此在dfs的基础上加上两次剪枝解决了这道题. 题意: 我没有读题,只是队友给我解释了题意,然后我根据题意写的题. 大概意思是给n个字符串,从上到下依次标记为1——n,寻找一个标记最大的串,要求这个串满足:标记比它小的串中至少有一个不是它的子串. 输入: 第一行输入一个整型t,表示共有t组数据. 每组数据首行一个整型n,表示有n个串. 接下来n行,每行

HDU 5510 Bazinga (字符串匹配)

题目:传送门. 题意:t 组数据,每组 n 个串,对于第 i 个串如果存在 1 到 i-1 中的某个串不是 i 的子串,那么这个第 i 个串符合题意,求 i 的最大值. 题解:KMP,AC自动机也可以,直接匹配就行.注意如果串 j 是j+1的子串,那么对于j+2来说只需要匹配j+1是不是他的子串即可,因为比匹配 j 更容易符合题意.用cin会超时,要用scanf. #include <iostream> #include <cstdio> #include <cmath>