Hdu5510 Bazinga
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$,表示字符串的 ...
随机推荐
- qt 程序国际化
http://www.cnblogs.com/hujian/archive/2012/08/10/2631488.html
- j2ee学习笔记URLEncoder.encode(String , enc)处理特殊字符
从页面上传递数据时,特殊字符对于超链接会自动进行解码 : 其他方式需要手动进行解码.
- android开发之Parcelable使用详解
想要在两个activity之间传递对象,那么这个对象必须序列化,android中序列化一个对象有两种方式,一种是实现Serializable接口,这个非常简单,只需要声明一下就可以了,不痛不痒.但是a ...
- gson使用详解
昨天读一篇文章,看到gson这个词,一开始还以为作者写错了,问了度娘之后才发现是我才疏学浅,于是大概了解了一下gson用法,总体来说还是很简单的. Gson.jar下载 JavaBean转json / ...
- Android with Eclipse - Waiting for HOME ('android.process.acore') to be launched?
mac机中使用命令行方式启动android sdk manager,有需要的朋友可以参考下. 相信使用mac机的用户做android开发都会有一个困惑,就是如何更新android sdk,或者说直接使 ...
- 定位 position
html结构是fixed包裹relative,relative包裹absolute position:relative;相对定位 a 不影响元素本身的特性 b 不使元素脱离文档流(元素移动之后原始位置 ...
- hibernate和mybatis思想,区别,优缺点
Hibernate 简介 Hibernate对数据库结构提供了较为完整的封装,Hibernate的O/R Mapping实现了POJO 和数据库表之间的映射,以及SQL 的自动生成和执行.程序员往往只 ...
- 《你不常用的c#之三》:Action 之怪状
转载自csdn:http://blog.csdn.net/robingaoxb/article/details/6199891 例1: public static void Main() { Li ...
- Eclipse闪退/打不开/无法启动/一闪而过
转自:http://my.oschina.net/psuyun/blog/421058 很长时间了,写java.写android都是用的Eclipse.可是突然有一天,当我像往常一样试图打开Eclip ...
- C#缓存处理
第一种方式: 在ASP.NET中页面缓存的使用方法非常的简单,只需要在aspx页的顶部加这样一句声明即可: <%@ OutputCache Duration="60" Var ...