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可以减少一半时间
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; int T, n, use[maxn], mx, ans;
char s[][];
std::vector<int> v;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&T)){
for(int kase=;kase<=T;kase++){
ans=-;
scanf("%d",&n);
mx=;
use[]=;
v.clear();
scanf("%s",s[]);
for(int i=;i<=n;i++){
scanf("%s",s[i]);
int flag=;
if(strstr(s[i],s[mx])==NULL)
flag=;
if(flag&&use[i-]){
for(int j=v.size()-;j>= && flag;j--){
if(strstr(s[i],s[v[j]])==NULL)
flag=;
}
}
if(flag){
mx=i; use[i]=; v.clear();
}else{
ans=i; use[i]=; v.push_back(i);
}
} printf("Case #%d: %d\n",kase,ans);
}
}
return ;
}

HDU_5510_Bazinga的更多相关文章

随机推荐

  1. HDU 1863 畅通工程 克鲁斯卡尔算法

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. firewalld增加端口访问权限

    firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload

  3. [C++]在什么时候需要“#include string.h“

    相关资料:https://zhidao.baidu.com/question/515578726.html C++中,string头文件基本上已经包含在iostream中了.但是,平时使用的时候建议加 ...

  4. LT和ET模式

    #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include &l ...

  5. linphone 调试信息

    root@phyCORE-AM335x:~ linphonec -V -d 6INFO: no logfile, logging to stdoutortp-message-oRTP-0.20.0 i ...

  6. 巨头们的GitHub仓库整理

    1.Google >1.Google >https://github.com/google >2.Google Samples https://github.com/googlesa ...

  7. EasyUI Window和Layout

    我们建立tabs内容. <div class="easyui-window" title="Layout Window" icon="icon- ...

  8. 关于Unity的组件和作用

    一.Transform组件 整个场景由节点树组成. 节点+Transform组件,每个Transform有自己的孩子Transform,由Transform组成Transform树,而每个Transf ...

  9. Sencha Touch快速入门(译)

    翻译自:http://www.sencha.com/learn/sencha-touch-quick-start/ 1.下载Sencha Touch SDK——下载链接 2.安装Safari或Chro ...

  10. 【BZOJ】1630: [Usaco2007 Demo]Ant Counting(裸dp/dp/生成函数)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1630 题意,给你n种数,数量为m个,求所有的数组成的集合选长度l-r的个数 后两者待会写.. 裸dp ...