Hdu5510 Bazinga

时间:2022-09-12 14:25:29

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&lpar;HDU5510&plus;KMP&rpar;

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

  2. HDU 5510 Bazinga &lpar;2015沈阳现场赛,子串判断&rpar;

    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&lpar;字符串kmp&rpar;

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

  5. Bazinga HDU 5510 Bazinga&lpar;双指针&rpar;

    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 字符串&plus;哈希

    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【技巧暴力&plus;字符串】

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

随机推荐

  1. ZOJ 3871 Convex Hull(计算几何、凸包)

    题意:给n个点,|x[i]|,|y[i]| <= 1e9.求在所有情况下的子集下(子集点数>=3),凸包的面积和. 这题主要有几个方面,一个是凸包的面积,可以直接用线段的有向面积和求得,这 ...

  2. MySQL流程函数

    #如果value是真,返回t:否则返回f if(value,t f) #如果value1不为空,返回value1,否则返回value2 ifnull(value1,value2) #如果value1是 ...

  3. nginx&colon; &lbrack;warn&rsqb; conflicting server name &quot&semi;localhost&quot&semi; on 0&period;0&period;0&period;0&colon;80&comma; ignored

    修改nginx配置参数后,使用nginx -t检查配置. 提示successfull后就可以使用 nginx -s reload来重新加载配置 我配置的过程中遇到这样的问题,就是绑定了主机名后,重新加 ...

  4. 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 ...

  5. tabBaritem的图片偏移

    tabBarItem.imageInsets = UIEdgeInsetsMake(-10, 0, 10, 0);

  6. tensorflow Sigmoid 应用

    1.函数 函数:f(z) = 1 / (1 + exp( − z)) 导数:f(z)' = f(z)(1 − f(z)) 2.tensorflow实现 #!/usr/bin/env python # ...

  7. Java中String、StringBuilder、StringBuffer的区别

    常量还是变量: String是字符串常量(以final修饰符进行修饰,不可更改): StringBuilder是字符串变量 StringBuffer是字符串变量 线程安全: String无所谓线程安全 ...

  8. 15 Django REST Framework 给api添加自定义搜索条件

    一.ListModelMixin源码 # 源码 class ListModelMixin(object): """ List a queryset. "&quo ...

  9. Left join update和 ROW&lowbar;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_ ...

  10. G 最水的一道

    G - Here Be Dragons The Triwizard Tournament's third task is to negotiate a corridor of many segment ...