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. python 进程复习

    import os import time ret = os.fork() # 创建子线程 if ret ==0: # 子进程中返回值为0,父进程>0 while True: print('.. ...

  2. 过滤器Filter与监听器Listener

    过滤器Filter 过滤器也是一种servlet   它也可以对用户的请求进行处理  , 但是他所做的处理,只是一些轻量级的处理.Fileter就好像jsp页面与servlet之间的一道关卡,如果这个 ...

  3. Apache的配置httpd.conf文件配置

    (1) 基本配置: ServerRoot "/mnt/software/apache2" #你的apache软件安装的位置.其它指定的目录如果没有指定绝对路径,则目录是相对于该目录 ...

  4. 释义Oracle 11r2中并行执行相关参数

    因最近对现场某些服务器进行诊断和调整,用到了这类参数,因此对这类参数做了详尽的查阅和研究,现将该类参数释义如下,以方便同行和自己参考,禁止转载: 1.PARALLEL_ADAPTIVE_MULTI_U ...

  5. JAVA工程师面试题【来自并发编程网】

    基础题: Java线程的状态 进程和线程的区别,进程间如何通讯,线程间如何通讯 HashMap的数据结构是什么?如何实现的.和HashTable,ConcurrentHashMap的区别 Cookie ...

  6. javascript中获取dom元素的高度和宽度

    javascript中获取dom元素高度和宽度的方法如下: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网 ...

  7. Linux入门:usermod - 修改用户帐户信息

    一.什么是usermod? usermod 命令通过修改系统帐户文件来修改用户账户信息usermod [options] user_name选项(options)-a|--append ##把用户追加 ...

  8. Docker学习实践 - Docker安装MySql数据库

    Docker安装MySQL数据库 1.Ubuntu安装MySQL安装 (1)安装编译源码需要的包 sudo apt-get install make cmake gcc g++ bison libnc ...

  9. jquery中attr与prop的区别

    先从一个老生常谈的问题说起,使用jquery实现全选全不选.楼主先使用的jquery版本是 jquery-1.11.1.min.js 全选<input type="checkbox&q ...

  10. MySQL/上

    MySQL操作/上 一.视图 视图表是一个虚拟表(非真实存在),其本质是[根据sql语句获取动态的数据集,并为其命名],用户使用表只需使用(名称)即可获取结果集,并可以将其当做表来使用. 1.创建视图 ...