Bazinga

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

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
 
  • 找到最大ID字符串i使得存在j<i的字符串不是i的子串
  • 首先考虑如果从l到r串都满足都是r的子串,那么k>r串只需要检测r串即可
  • 如果自l串开始到l+k串都是存在小于l的串不是当前串子串,那么对于l+k+1串就要检测l-1串和l到l+k串
  • 算法出来了,就是记录当前最大id使得此id之前全是id串的子串,然后检测串的范围就是这个id到当前ID的前一位
  • 如果用这样的算法,用c的strstr即可,用kmp可以减少一半时间
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cmath>
  8. #include <vector>
  9. #include <queue>
  10. #include <stack>
  11. #include <set>
  12. #include <map>
  13. using namespace std;
  14. typedef long long LL ;
  15. typedef unsigned long long ULL ;
  16. const int maxn = 1e5 + ;
  17. const int inf = 0x3f3f3f3f ;
  18. const int npos = - ;
  19. const int mod = 1e9 + ;
  20. const int mxx = + ;
  21. const double eps = 1e- ;
  22. const double PI = acos(-1.0) ;
  23.  
  24. int T, n, use[maxn], mx, ans;
  25. char s[][];
  26. std::vector<int> v;
  27. int main(){
  28. // freopen("in.txt","r",stdin);
  29. // freopen("out.txt","w",stdout);
  30. while(~scanf("%d",&T)){
  31. for(int kase=;kase<=T;kase++){
  32. ans=-;
  33. scanf("%d",&n);
  34. mx=;
  35. use[]=;
  36. v.clear();
  37. scanf("%s",s[]);
  38. for(int i=;i<=n;i++){
  39. scanf("%s",s[i]);
  40. int flag=;
  41. if(strstr(s[i],s[mx])==NULL)
  42. flag=;
  43. if(flag&&use[i-]){
  44. for(int j=v.size()-;j>= && flag;j--){
  45. if(strstr(s[i],s[v[j]])==NULL)
  46. flag=;
  47. }
  48. }
  49. if(flag){
  50. mx=i; use[i]=; v.clear();
  51. }else{
  52. ans=i; use[i]=; v.push_back(i);
  53. }
  54. }
  55.  
  56. printf("Case #%d: %d\n",kase,ans);
  57. }
  58. }
  59. return ;
  60. }

HDU_5510_Bazinga的更多相关文章

随机推荐

  1. 452. Remove Linked List Elements【Naive】

    Remove all elements from a linked list of integers that have value val. Example Given 1->2->3- ...

  2. 01、Windows Phone 套接字(Socket)实战之交互设计

    这个 Demo 主要使用 WP 中提供的 Socket 对象,来与 PC 端进行文字.文件的互相传输.因为在 WP 中系统 对存储的操作限制的比较多,例如,你把 .doc..txt..zip 等常见的 ...

  3. CentOS 6.2修改主机名

    写在前面的话:因为服务器要统一主机名,但是在安装的时候忘记设置了,所以需要修改主机名   需要修改两处:一处是/etc/sysconfig/network,另一处是/etc/hosts,只修改任一处会 ...

  4. c语言中struct的内存对齐

    为了让CPU能够更舒服地访问到变量,struct中的各成员变量的存储地址有一套对齐的机制.这个机制概括起来有两点:第一,每个成员变量的首地址,必须是它的类型的对齐值的整数倍,如果不满足,它与前一个成员 ...

  5. hdu1978(记忆化搜索)

    #include<iostream> #include<stdio.h> #include<string.h> #include<queue> usin ...

  6. windows测试模式

    测试模式通常意义就是让windows 操作系统在测试状态下运行,windows操作系统在这种模式下可以运行非官方或无数字签名的驱动程序 .   目录 1 定义 2 进入/退出windows测试模式方法 ...

  7. spring中事务传播解读:PROPAGATION_REQUIRES_NEW

    第一步:获取事务状态,判断当前事务线程是否存在.第二步:如果当前事务的传播行为为PROPAGATION_REQUIRES_NEW,挂起当前线程绑定的事务,取消当前事务的sessionHolder和co ...

  8. Excel 自定义关闭按钮

    遇到过这样一个需求,是在excel关闭的时候,不要excel本身的保存窗口,只用自定义的. 这个的需要第一,是点击关闭时候触发, 第二:触发后,不能还是弹出那个窗口 第三:取消后,要能停止程序 为了弄 ...

  9. oozie开发注意事项

    ooziejob执行后 1. job.properties.coordinatior.xml中设置的值都是不可变的,除非将job kill掉,然后重新调度. oozie job -kill 00000 ...

  10. SSIS 自测题-文件操作类

    说明:以下是自己的理解答案,不是标准的答案,如有不妥烦请指出.         有些题目暂时没有答案,有知道的请留言,互相学习,一起进步. 1.什么是控制流,什么是数据流,控制流和数据流之间的关系是什 ...