HDU 5510 Bazinga (2015沈阳现场赛,子串判断)
Bazinga
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 214 Accepted Submission(s): 89
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".
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.
5
ab
abc
zabc
abcd
zabcd
4
you
lovinyou
aboutlovinyou
allaboutlovinyou
5
de
def
abcd
abcde
abcdef
3
a
ba
ccc
Case #2: -1
Case #3: 4
Case #4: 3
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define eps 1e-8
#define zero(x)(((x)>0?(x):-(x))<eps)
#define PI acos(-1.0)
#define LL long long
#define maxn 2200
#define IN freopen("in.txt","r",stdin);
using namespace std; char Str[][];
/*KMP--字符串匹配--单一模版串*/
//char str[maxn],p[maxn];/*p is template string*/
int f[maxn],cnt;//when failed,get to f[i];
void getf(char *p)
{
memset(f,,sizeof(f));
int len=strlen(p); /* only cal once, or TLE */
for(int i=;i<len;i++)
{
int j=f[i];
while(j&&p[i]!=p[j]) j=f[j];
f[i+]=(p[i]==p[j]? j+:);
}
}
int kmp(char *str,char *p) /*O(len1+len2)*/
{
getf(p);
cnt=;
int len1=strlen(str),len2=strlen(p); /* only cal once, or TLE*/
for(int i=,j=;i<len1;i++)
{
while(j&&str[i]!=p[j]) j=f[j];
if(str[i]==p[j]) j++;
if(j==len2) cnt++;/*Match success*/
if(cnt!=) break;
}
return cnt;
} int main(int argc, char const *argv[])
{
//IN; int t,ca=;scanf("%d",&t);
while(t--)
{
int n;scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%s",Str[i]); int flag[maxn]={};
for(int i=;i<n;i++)
if(kmp(Str[i+],Str[i])!=) flag[i]=; for(int i=n;i>=;i--){
for(int j=;j<i;j++){
if(flag[j]) continue;
kmp(Str[i],Str[j]);
if(cnt==){
printf("Case #%d: %d\n",ca++,i);goto s;
}
}
} printf("Case #%d: -1\n",ca++);
s:continue;
} return ;
}
HDU 5510 Bazinga (2015沈阳现场赛,子串判断)的更多相关文章
- HDU 5512 Pagodas (2015沈阳现场赛,找规律+gcd)
Pagodas Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 5531 Rebuild (2015长春现场赛,计算几何+三分法)
Rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- 【HDU 5510 Bazinga】字符串
2015沈阳区域赛现场赛第2题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:给定一个由字符串组成的序列,一共n个元素,每个元素是一个不 ...
- hdu 5510 Bazinga(字符串kmp)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Bazinga HDU 5510 Bazinga(双指针)
Bazinga HDU 5510 Bazinga(双指针) 题链 解法:对于串i来说,如果串i是不符合的,那么代表串i之前的字符串都是i的子串,那么我们求一个新的i(定义为ti),如果i是ti 的子串 ...
- hdu 5510 Bazinga (kmp+dfs剪枝) 2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
废话: 这道题很是花了我一番功夫.首先,我不会kmp算法,还专门学了一下这个算法.其次,即使会用kmp,但是如果暴力枚举的话,还是毫无疑问会爆掉.因此在dfs的基础上加上两次剪枝解决了这道题. 题意: ...
- hdu 5455 (2015沈阳网赛 简单题) Fang Fang
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5455 题意就是找出所给字符串有多少个满足题目所给条件的子串,重复的也算,坑点是如果有c,f以外的字符也是不 ...
- hdu 5461(2015沈阳网赛 简单暴力) Largest Point
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5461 题意就是在数组中找出a*t[i]*t[i]+b*t[j]的最大值,特别注意的是这里i和i不能相等,想 ...
- hdu 5459(2015沈阳网赛) Jesus Is Here
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5459 题意 给出一组字符串,每个字符串都是前两个字符串相加而成,求第n个字符串的c的各个坐标的差的和,结果 ...
随机推荐
- IOSTimer的例子留个备注
1.创建一个定时器 ,以下是便利构造器方法,+ scheduledTimerWithTimeInterval:invocation:repeats:+ scheduledTimerWithTimeIn ...
- [LeetCode#247] Strobogrammatic Number II
Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...
- hdu 4911 Inversion (分治 归并排序 求逆序数)
题目链接 题意:给n个数,求交换k次相邻的数之后的最小的逆序数对. 用分治的方法,以前在poj上做过这种题,昨天比赛的时候忘了.... 下面的归并排序还是以前的模板. #include <ios ...
- 函数xdes_calc_descriptor_page
根据偏移量计算出第几个xdes page 0 %16328 = 0 64% 16328 = 64 128 % 16328 = 128 192 % 16328 = 192 /************** ...
- int和integer;Math.round(11.5)和Math.round(-11.5)
int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为int提供的封装类.int的默认值为0,而Integer的默认值为null,即Integer可 ...
- JSOI2008最大数(线段树)
注意到数列只增不减,而题目中又明确说道m<=200000;这样的数据规模线段树完全可以承受得了.所以我们可以事先建好一棵200000个子节点的线段树,然后求极值就好了. type node=re ...
- Websocket和PHP Socket编程
本来是搜一些html5 websocket资料看的,结果被引去看了php的socket编程.下面是一些简单的例子,在命令行运行php脚本就行 [命令行运行PHP]PHP中有一个php.exe文件,可以 ...
- busybox filesystem udhcpc 原理
/******************************************************************** * busybox filesystem udhcpc 原理 ...
- Svn正确的使用方法
想必大家现在都比较喜欢使用svn(subversion)完成代码管理了,因为它的开源,轻巧,易用.但是这样一个宝贝如果不知道其正确的用法,也会让我们百思不得其解,甚至耽误项目进度,浪费程序员的心血和结 ...
- 用ffmpeg把H264数据流解码成YUV420P
在网上找了很久这方面的内容,发现网上的代码都太旧了,所使用的函数旧到连最新版本的ffmpeg都已经不包含了,所以对于我这个初学者来说太坑拉.不过经过多次查找ffmpeg的头文件和结合网上的内容,终于成 ...