Description
Ladies and gentlemen, please sit up straight.
Don't tilt your head. I'm serious.
For \(n\) given strings \(S_{1},S_{2},\cdots,S_{n}\), labelled from \(1\) to \(n\), you should find the largest \(i (1 \le i \le n)\) such that there exists an integer \(j (1 \le j < i)\) and \(S_{j}\) is not a substring of \(S_{i}\).
A substring of a string \(S_{i}\) is another string that occurs in \(S_{i}\). 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 \le t \le 50)\) which is the number of test cases.
For each test case, the first line is the positive integer \(n\) \((1\le n \le 500)\) and in the following n lines list are the strings \(S_{1},S_{2},\cdots,S_{n}\).
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
开始做的姿势不对,想的是裸暴力或者什么ac自动机啊。。。。(I 'm so puny!!!)
其实这题正解就是暴力的剪枝,想如果\(S_{j}\)是\(S_{i}\)的子串(\(i > j\)),那么对于\(k > i\),若\(S_{i}\)是\(S_{k}\)的子串,那么\(S_{j}\)一定也是\(S_{k}\)的子串;如果不是\(k\)就可以更新答案,那么也就说明只需要匹配\(i\)而不要匹配\(j\),由此可以打个\(vis\)标记剪枝了。
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
#define maxn (510)
#define maxl (2010)
#define rhl (5000011)
#define xi (127)
int T,N,len[maxn],mi[maxl],pre[maxn][maxl],ans;
char s[maxl]; bool exist[maxn];
inline bool find(int a,int b)
{
for (int i = len[b];i <= len[a];++i)
{
int key = pre[a][i]-(ll)pre[a][i-len[b]]*(ll)mi[len[b]]%rhl;
if (key < 0) key += rhl; if (key == pre[b][len[b]]) return true;
}
return false;
}
int main()
{
freopen("5510.in","r",stdin);
freopen("5510.out","w",stdout);
scanf("%d",&T); mi[0] = 1;
for (int i = 1;i <= 2000;++i) mi[i] = (mi[i-1]*xi)%rhl;
for (int Cas = 1;Cas <= T;++Cas)
{
printf("Case #%d: ",Cas);
memset(exist,false,sizeof(exist));
scanf("%d",&N); ans = 0;
for (int i = 1;i <= N;++i)
{
scanf("%s",s+1); len[i] = strlen(s+1);
for (int j = 1;j <= len[i];++j) pre[i][j] = (pre[i][j-1]*xi+s[j]-'a'+1)%rhl;
for (int j = i-1;j;--j)
{
if (exist[j]) continue;
if (find(i,j)) exist[j] = true; else ans = i;
}
}
if (ans) printf("%d\n",ans); else puts("-1");
}
fclose(stdin); fclose(stdout);
return 0;
}
Hdu5510 Bazinga的更多相关文章
-
Bazinga(HDU5510+KMP)
t题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题目: 题意:找到一个编号最大的字符串满足:存在一个编号比它小的字符串不是它的字串. 思路:K ...
-
HDU 5510 Bazinga (2015沈阳现场赛,子串判断)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
-
HDU 5510 Bazinga 暴力匹配加剪枝
Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...
-
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)
http://acm.hdu.edu.cn/showproblem.php?pid=5510 Bazinga Problem Description Ladies and gentlemen, p ...
-
TTTTTTTTTTTTTTTT hdu 5510 Bazinga 字符串+哈希
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
-
Bazinga
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
-
Bazinga HDU - 5510【技巧暴力+字符串】
题目:https://vjudge.net/problem/HDU-5510 $2015ACM/ICPC$ 亚洲区沈阳站 题目大意: 输入$t$(表示样例个数) 如何每个样例一个 $n$,表示字符串的 ...
随机推荐
-
ZOJ 3871 Convex Hull(计算几何、凸包)
题意:给n个点,|x[i]|,|y[i]| <= 1e9.求在所有情况下的子集下(子集点数>=3),凸包的面积和. 这题主要有几个方面,一个是凸包的面积,可以直接用线段的有向面积和求得,这 ...
-
MySQL流程函数
#如果value是真,返回t:否则返回f if(value,t f) #如果value1不为空,返回value1,否则返回value2 ifnull(value1,value2) #如果value1是 ...
-
nginx: [warn] conflicting server name ";localhost"; on 0.0.0.0:80, ignored
修改nginx配置参数后,使用nginx -t检查配置. 提示successfull后就可以使用 nginx -s reload来重新加载配置 我配置的过程中遇到这样的问题,就是绑定了主机名后,重新加 ...
-
ACM 最少步数
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
-
tabBaritem的图片偏移
tabBarItem.imageInsets = UIEdgeInsetsMake(-10, 0, 10, 0);
-
tensorflow Sigmoid 应用
1.函数 函数:f(z) = 1 / (1 + exp( − z)) 导数:f(z)' = f(z)(1 − f(z)) 2.tensorflow实现 #!/usr/bin/env python # ...
-
Java中String、StringBuilder、StringBuffer的区别
常量还是变量: String是字符串常量(以final修饰符进行修饰,不可更改): StringBuilder是字符串变量 StringBuffer是字符串变量 线程安全: String无所谓线程安全 ...
-
15 Django REST Framework 给api添加自定义搜索条件
一.ListModelMixin源码 # 源码 class ListModelMixin(object): """ List a queryset. "&quo ...
-
Left join update和 ROW_NUMBER
(1)Left join 更新update a set a.name=b.name from a left join b on a.id=b.stuid(2)ROW_NUMBERselect ROW_ ...
-
G 最水的一道
G - Here Be Dragons The Triwizard Tournament's third task is to negotiate a corridor of many segment ...