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.
![](http://acm.hdu.edu.cn/data/images/C645-1002-1.jpg)
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的各个坐标的差的和,结果 ...
随机推荐
- heatmap.2
heatmap.2 {gplots} R Documentation Enhanced Heat Map Description A heat map is a false color image ( ...
- sscanf() 和 sprintf()的用法。
因为感觉比较有用. 这几次比赛,用过几次,所以写个程序,总结一下. 如果用sscanf(s, "%d.%d", &a, &b); 的时候,一定要注意是否s里一定有小 ...
- open_table
/* Open a table. SYNOPSIS open_table() thd Thread context. table_list Open first table in list. acti ...
- 函数xdes_set_bit
/**********************************************************************//** Sets a descriptor bit of ...
- UVa 540 Team Queue 【STL】
题意:给出t个团体,这t个团体排在一起,每次新来一个x排队,如果在整个的团体队列中,有x的队友,那么x排在它的队友的后面,如果他没有队友,则排在长队的队尾 求给出的每一个出队命令,输出出队的人的编号 ...
- sql字符串函数(转)
计算字符串长度 len()用来计算字符串的长度 select sname ,len(sname) from student 字符串转换为大.小写 lower() 用来将一个字符串转换为小写,upper ...
- shape的属性(二)
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="r ...
- UVA 11294 Wedding(2-sat)
2-sat.不错的一道题,学到了不少. 需要注意这么几点: 1.题目中描述的是有n对夫妇,其中(n-1)对是来为余下的一对办婚礼的,所以新娘只有一位. 2.2-sat问题是根据必然性建边,比如说A与B ...
- [Sciter系列] MFC下的Sciter–4.HTML与图片资源内置
[Sciter系列] MFC下的Sciter–4.HTML与图片资源内置,防止代码泄露. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的SciterFrame程序,以此作 ...
- malloc、free的使用
一.malloc()和free()的基本概念以及基本用法: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针 ...