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 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
/*
hdu 5510 Bazinga(字符串kmp) problem:
给你n个字符串,求一个最大的序号i. 使1~i-1的字符串中有一个不是str[i]的子串 solve:
最开始看见想的是字典树.但是感觉很麻烦,目测会超时.
然后想的是kmp,但是优化有问题. 每次倒着往前搜,想的是如果1~i-1的所有都是i的子串. 那么搜索到i时就可以直接返回true.
如果不匹配就退出,否则就一直搜索下去. 结果TLE了....很悲伤 后来发现正确优化:如果i是k的子串. 那么匹配的时候就不需要匹配i,因为如果当前与k匹配,那么就一定与i匹配....
所以过程中标记一下不用匹配的就行了
hhh-2016-08-29 21:00:19
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <set>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfs(a) scanf("%s",a)
#define scanfl(a) scanf("%I64d",&a)
#define key_val ch[ch[root][1]][0]
#define inf 1e9
using namespace std;
const ll mod = 1e9+7;
const int maxn = 2005;
char str[505][2004];
int flag[maxn];
void pre_kmp(char x[],int m,int kmpnext[])
{
int i,j;
j = kmpnext[0] = -1;
i = 0;
while(i < m)
{
while(j != -1 && x[i] != x[j])
j = kmpnext[j];
if(x[++i] == x[++j])
kmpnext[i] = kmpnext[j];
else
kmpnext[i] = j;
}
}
int nex[2005];
int kmp(char x[],char y[])
{
int m = strlen(x);
int n = strlen(y);
int i,j;
clr(nex,0);
pre_kmp(x,m,nex);
i = j = 0; while(i < n)
{
while(j != -1 && y[i] != x[j]) j = nex[j];
i++,j++;
if(j >= m)
{
return true;
}
}
return false;
} int main()
{
int T,n;
// freopen("in.txt","r",stdin);
scanfi(T);
int cas = 1;
while(T--)
{
clr(flag,0);
scanfi(n);
int ans = -1;
for(int i = 1; i <= n; i++)
{
scanfs(str[i]);
for(int j =1;j < i;j++)
{
if(flag[j])
continue;
if(kmp(str[j],str[i]))
flag[j] = 1;
else
{
ans = i;
}
}
}
printf("Case #%d: %d\n",cas++,ans);
}
return 0;
}

  

hdu 5510 Bazinga(字符串kmp)的更多相关文章

  1. TTTTTTTTTTTTTTTT hdu 5510 Bazinga 字符串+哈希

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  2. Bazinga HDU 5510 Bazinga(双指针)

    Bazinga HDU 5510 Bazinga(双指针) 题链 解法:对于串i来说,如果串i是不符合的,那么代表串i之前的字符串都是i的子串,那么我们求一个新的i(定义为ti),如果i是ti 的子串 ...

  3. hdu 5510 Bazinga KMP+尺取法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:至多50组数据,每组数据至多500个字符串,每个字符串的长度最长为2000.问最大的下标( ...

  4. 【HDU 5510 Bazinga】字符串

    2015沈阳区域赛现场赛第2题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:给定一个由字符串组成的序列,一共n个元素,每个元素是一个不 ...

  5. hdu 5510 Bazinga (KMP+暴力标记)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 思路: 一开始直接用KMP莽了发,超时了,后面发现如果前面的字符串被后面的字符串包含,那么我们就 ...

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

    废话: 这道题很是花了我一番功夫.首先,我不会kmp算法,还专门学了一下这个算法.其次,即使会用kmp,但是如果暴力枚举的话,还是毫无疑问会爆掉.因此在dfs的基础上加上两次剪枝解决了这道题. 题意: ...

  7. HDU 5510 Bazinga KMP

    题意: 给\(n(1 \leq n \leq 500)\)个字符串,求一个最大的\(i\),使得存在一个\(S_{j}\)不是\(S_i\)的子串. 分析: 维护两个指针\(l,r\) 那么有两种情况 ...

  8. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  9. HDU 5510 Bazinga (2015沈阳现场赛,子串判断)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

随机推荐

  1. C语言博客-指针

    一.PTA实验作业(5分) 题目1:6-1 两个4位正整数的后两位互换 1. 本题PTA提交列表 2. 设计思路 3.代码截图 4.本题调试过程碰到问题及PTA提交列表情况说明. 无 题目2:6-3 ...

  2. PTA題目的處理(一)

    **題目1:A乘B** **實驗代碼** #include <stdio.h> #include <stdlib.h> int main() { signed int a,b; ...

  3. 201621123043 《Java程序设计》第2周学习总结

    1.本周学习总结 使用jdk文档查阅函数功能及代码 用switch语句是在每个case中可能在第一行是sc.nextLine;来给回车赋值: 在使用循环的时候要注意循环返回的条件,否则陷入死循环可能会 ...

  4. JS页面跳转的常用方法整理.

    <script type="text/javascript"> //js页面跳转 function showtabs() { window.location.href ...

  5. 使用JDBC中的出现的乱码和查询无结果问题

    使用JDBC中的问题 连接的后出现查询结果是乱码. 1.可能是代码的编码与数据库的编码不同 ​ 有可以将二者都设置为UTF-8 2.如果比较懒得话可以只设代码为UTF-8 mysql 连接url中us ...

  6. tomca配置文件自动还原问题的解决 server.xml content.xml 等

    当我们在处理中文乱码或是配置数据源时,我们要修改Tomcat下的server.xml和content.xml文件. 但是当我们修改完后重启Tomcat服务器时发现xml文件又被还原了,修改无效果. 为 ...

  7. EasyUI中datagrid的基本用法

    EasyUI中datagrid是最常用的一个控件了,现在整理一下datagrid的基本语法,先展示下页面效果吧,如下图

  8. C#实现导出Excel

    这段时间用到了导出Excel的功能,这个功能还是比较常用的,我常用的有两个方法,现在整理一下,方便以后查看. 一.实现DataTable数据导出到本地,需要自己传进去导出的路径. /// <su ...

  9. ssh_maven的搭建之dao层的开发

    之前都是使用我们传统的方式进行引入jar包,现在我们使用maven进行管理依赖,这样,我们的jar就不需要我们进行管理,而且,我们的maven还可以进行项目构建,一个项目从编写源代码到编译,测试,运行 ...

  10. Android fragment切换后onresume时报 Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim'

    动态加载fragment以后,调用了remove方法移除Fragment,在返回来的时候报 Attempt to write to field 'int android.support.v4.app. ...