Bazinga

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

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
 
Source
 
 
【题意】:
n个字符串,找出最大的i,使得存在j 满足j<i 且Sj不是Si的字串。
 
【解题思路】:
看着是个水题本想直接上,看到一片TLE吓得上了个KMP的板。。。
交了一发还是TLE,已经不想做了。。
在某blog的指点下,先判断相邻两个串是否存在字串关系,若Si是Sj的字串,则i和j只需要kmp()一次即可~
减少kmp()次数后800ms过了,,感觉还是不够优美
搜了一下发现有各种做法,Hash、dp......
有空再改,挖坑待填~~
 
(代码写得丑==)
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<string>
  6. #include<algorithm>
  7. #define eps 1e-8
  8. #define zero(x)(((x)>0?(x):-(x))<eps)
  9. #define PI acos(-1.0)
  10. #define LL long long
  11. #define maxn 2200
  12. #define IN freopen("in.txt","r",stdin);
  13. using namespace std;
  14.  
  15. char Str[][];
  16. /*KMP--字符串匹配--单一模版串*/
  17. //char str[maxn],p[maxn];/*p is template string*/
  18. int f[maxn],cnt;//when failed,get to f[i];
  19. void getf(char *p)
  20. {
  21. memset(f,,sizeof(f));
  22. int len=strlen(p); /* only cal once, or TLE */
  23. for(int i=;i<len;i++)
  24. {
  25. int j=f[i];
  26. while(j&&p[i]!=p[j]) j=f[j];
  27. f[i+]=(p[i]==p[j]? j+:);
  28. }
  29. }
  30. int kmp(char *str,char *p) /*O(len1+len2)*/
  31. {
  32. getf(p);
  33. cnt=;
  34. int len1=strlen(str),len2=strlen(p); /* only cal once, or TLE*/
  35. for(int i=,j=;i<len1;i++)
  36. {
  37. while(j&&str[i]!=p[j]) j=f[j];
  38. if(str[i]==p[j]) j++;
  39. if(j==len2) cnt++;/*Match success*/
  40. if(cnt!=) break;
  41. }
  42. return cnt;
  43. }
  44.  
  45. int main(int argc, char const *argv[])
  46. {
  47. //IN;
  48.  
  49. int t,ca=;scanf("%d",&t);
  50. while(t--)
  51. {
  52. int n;scanf("%d",&n);
  53. for(int i=;i<=n;i++) scanf("%s",Str[i]);
  54.  
  55. int flag[maxn]={};
  56. for(int i=;i<n;i++)
  57. if(kmp(Str[i+],Str[i])!=) flag[i]=;
  58.  
  59. for(int i=n;i>=;i--){
  60. for(int j=;j<i;j++){
  61. if(flag[j]) continue;
  62. kmp(Str[i],Str[j]);
  63. if(cnt==){
  64. printf("Case #%d: %d\n",ca++,i);goto s;
  65. }
  66. }
  67. }
  68.  
  69. printf("Case #%d: -1\n",ca++);
  70. s:continue;
  71. }
  72.  
  73. return ;
  74. }
 

HDU 5510 Bazinga (2015沈阳现场赛,子串判断)的更多相关文章

  1. HDU 5512 Pagodas (2015沈阳现场赛,找规律+gcd)

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

  2. HDU 5531 Rebuild (2015长春现场赛,计算几何+三分法)

    Rebuild Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total S ...

  3. 【HDU 5510 Bazinga】字符串

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

  4. hdu 5510 Bazinga(字符串kmp)

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

  5. Bazinga HDU 5510 Bazinga(双指针)

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

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

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

  7. hdu 5455 (2015沈阳网赛 简单题) Fang Fang

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5455 题意就是找出所给字符串有多少个满足题目所给条件的子串,重复的也算,坑点是如果有c,f以外的字符也是不 ...

  8. hdu 5461(2015沈阳网赛 简单暴力) Largest Point

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5461 题意就是在数组中找出a*t[i]*t[i]+b*t[j]的最大值,特别注意的是这里i和i不能相等,想 ...

  9. hdu 5459(2015沈阳网赛) Jesus Is Here

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5459 题意 给出一组字符串,每个字符串都是前两个字符串相加而成,求第n个字符串的c的各个坐标的差的和,结果 ...

随机推荐

  1. heatmap.2

    heatmap.2 {gplots} R Documentation Enhanced Heat Map Description A heat map is a false color image ( ...

  2. sscanf() 和 sprintf()的用法。

    因为感觉比较有用. 这几次比赛,用过几次,所以写个程序,总结一下. 如果用sscanf(s, "%d.%d", &a, &b); 的时候,一定要注意是否s里一定有小 ...

  3. open_table

    /* Open a table. SYNOPSIS open_table() thd Thread context. table_list Open first table in list. acti ...

  4. 函数xdes_set_bit

    /**********************************************************************//** Sets a descriptor bit of ...

  5. UVa 540 Team Queue 【STL】

    题意:给出t个团体,这t个团体排在一起,每次新来一个x排队,如果在整个的团体队列中,有x的队友,那么x排在它的队友的后面,如果他没有队友,则排在长队的队尾 求给出的每一个出队命令,输出出队的人的编号 ...

  6. sql字符串函数(转)

    计算字符串长度 len()用来计算字符串的长度 select sname ,len(sname) from student 字符串转换为大.小写 lower() 用来将一个字符串转换为小写,upper ...

  7. shape的属性(二)

    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="r ...

  8. UVA 11294 Wedding(2-sat)

    2-sat.不错的一道题,学到了不少. 需要注意这么几点: 1.题目中描述的是有n对夫妇,其中(n-1)对是来为余下的一对办婚礼的,所以新娘只有一位. 2.2-sat问题是根据必然性建边,比如说A与B ...

  9. [Sciter系列] MFC下的Sciter–4.HTML与图片资源内置

    [Sciter系列] MFC下的Sciter–4.HTML与图片资源内置,防止代码泄露. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的SciterFrame程序,以此作 ...

  10. malloc、free的使用

    一.malloc()和free()的基本概念以及基本用法: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针 ...