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的更多相关文章

  1. Bazinga(HDU5510+KMP)

    t题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题目: 题意:找到一个编号最大的字符串满足:存在一个编号比它小的字符串不是它的字串. 思路:K ...

  2. HDU 5510 Bazinga (2015沈阳现场赛,子串判断)

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

  3. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  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)

    http://acm.hdu.edu.cn/showproblem.php?pid=5510 Bazinga Problem Description   Ladies and gentlemen, p ...

  7. TTTTTTTTTTTTTTTT hdu 5510 Bazinga 字符串+哈希

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

  8. Bazinga

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

  9. Bazinga HDU - 5510【技巧暴力+字符串】

    题目:https://vjudge.net/problem/HDU-5510 $2015ACM/ICPC$ 亚洲区沈阳站 题目大意: 输入$t$(表示样例个数) 如何每个样例一个 $n$,表示字符串的 ...

随机推荐

  1. Makefile 入门与基本语法 分类: C/C++ ubuntu 2015-05-18 11:16 466人阅读 评论(0) 收藏

    在我看来,学会写简单的Makefile,阅读较复杂的makefile,是每一个Linux程序员都必须拥有的基本素质.Makefile可以自动识别哪些源文件被更改过,需要重新编译,那些不需要.从而节省大 ...

  2. 再次轻度破解EXE文件

    在经历股市多年的大起大落.大赚大赔之后.痛定思痛.深切感到在金融市场拼搏.必须建立健全交易纪律守则,严格运行. 这套完整的纪律守则,就是"交易系统". 在很多方面,它与一般的专家系 ...

  3. 从零開始开发Android版2048 (一)初始化界面

    自学Android一个月多了,一直在工作之余零零散散地看一些东西.感觉经常使用的东西都有些了解了,可是一開始写代码总会出各种奇葩的问题.感觉还是代码写得太少.这样继续杂乱地学习下去进度也太慢了,并且学 ...

  4. jdk和jre是什么?都有什么用?(转帖)

    jdk和jre是什么?都有什么用?(转帖) 文章分类:Java编程 大家肯定在安装JDK的时候会有选择是否安装单独的jre,一般都会一起安装,我也建议大家这样做.由于这样更能帮助大家弄清楚它们的差别: ...

  5. oracle13 触发器 变量

    触发器   触发器是指隐含的执行的存储过程.当定义触发器时,必须要指定触发的事件和触发的操作,常用的触发事件包括insert,update,delete语句,而触发操作实际就是一个pl/sql块.可以 ...

  6. CentOS 更新yum源

    公司买了一台刀片机服务器,安装的系统版本太低,导致yum源不合适,安装就会报错. 在网上找了好长时间,才发现是yum源的问题.   转载原文: 突然想起试试 Docker,在一台计算机上安装了 Cen ...

  7. Chapter 3. Installing Gradle 安装gradle

    3.1. Prerequisites Gradle requires a Java JDK or JRE to be installed, version 6 or higher (to check, ...

  8. ListView simpleAdapter的基本使用

    使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行.HashMap的每个键 值数据映射到布局文件中对应id的组件上.因为系统没有对 ...

  9. RedHat7搭建PHP开发环境(Zend Studio)

    下载Zend Studio # wget http://downloads.zend.com/studio-eclipse/13.0.1/ZendStudio-13.0.1-linux.gtk.x86 ...

  10. Linux查看某个文件被哪些进程PID占用

    比如查看test.sh这个文件被哪些进程占用,可以用: lsof | grep test.sh 或者 fuser -v test.sh