Bazinga

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3509    Accepted Submission(s): 1122

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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5960 5959 5958 5957 5956 

/*
用两个指针模拟kmp,暴力
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define N 505
using namespace std;
///
int t,n;
int ne[];
void getnext(char *s2)
{
int i=,j=-,l=strlen(s2);
ne[]=-;
while(i<l)
{
if(j==-||s2[i]==s2[j])
{
i++;j++;
if(s2[i]==s2[j])
ne[i]=ne[j];
else
ne[i]=j;
}
else
j=ne[j];
}
}
int kmp(char *s,char *s2)
{
memset(ne,,sizeof(ne));
getnext(s2);
int i=,j=,k=,len=strlen(s),l=strlen(s2);
while(i<len)
{
if(j==-||s[i]==s2[j])
i++,j++;
else
j=ne[j];
if(j==l)
k++,j=ne[j];
}
return k;
}
char s[N][]={""};
bool vis[N],f;
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
scanf("%d",&t);
int Case=;
while(t--)
{
memset(vis,true,sizeof vis);
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%s",s[i]);
printf("Case #%d: ",Case++);
for(int i=;i<=n;i++)
if(kmp(s[i],s[i-])>)
vis[i-]=false;
int f=;
for(int i=n;i>=&&!f;i--)
for(int j=;j<i&&!f;j++)
if(vis[j])
if(kmp(s[i],s[j])==)
{
printf("%d\n",i);
f=true;
}
if(!f)
puts("-1");
}
return ;
}

2015ACM/ICPC亚洲区沈阳站 B-Bazinga的更多相关文章

  1. 2015ACM/ICPC亚洲区沈阳站 Pagodas

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

  2. hdu 5510 Bazinga (kmp+dfs剪枝) 2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

    废话: 这道题很是花了我一番功夫.首先,我不会kmp算法,还专门学了一下这个算法.其次,即使会用kmp,但是如果暴力枚举的话,还是毫无疑问会爆掉.因此在dfs的基础上加上两次剪枝解决了这道题. 题意: ...

  3. 2015ACM/ICPC亚洲区沈阳站-重现赛 B - Bazinga (KMP)

    题意:给你\(n\)个字符串,\(s_1,s_2,...,s_n\),对于\(i(1\le i\le n)\),找到最大的\(i\),并且满足\(s_j(1\le j<i)\)不是\(s_i\) ...

  4. 2015ACM/ICPC亚洲区沈阳站

    5510 Bazinga 题意:给出n个字符串,求满足条件的最大下标值或层数 条件:该字符串之前存在不是 它的子串 的字符串 求解si是不是sj的子串,可以用kmp算法之类的. strstr是黑科技, ...

  5. 2015ACM/ICPC亚洲区沈阳站 Solution

    A - Pattern String 留坑. B - Bazinga 题意:找一个最大的i,使得前i - 1个字符串中至少不是它的子串 思路:暴力找,如果有一个串已经符合条件,就不用往上更新 #inc ...

  6. 2015ACM/ICPC亚洲区沈阳站 部分题解

    链接在这:http://bak.vjudge.net/contest/132442#overview. A题,给出a,b和n,初始的集合中有a和b,每次都可以从集合中选择不同的两个,相加或者相减,得到 ...

  7. 2015ACM/ICPC亚洲区沈阳站重现赛-HDU5512-Pagodas-gcd

    n pagodas were standing erect in Hong Jue Si between the Niushou Mountain and the Yuntai Mountain, l ...

  8. 2015ACM/ICPC亚洲区沈阳站-重现赛 M - Meeting (特殊建边,最短路)

    题意:有\(n\)个点,\(m\)个集合,集合\(E_i\)中的点都与集合中的其它点有一条边权为\(t_i\)的边,现在问第\(1\)个点和第\(n\)个点到某个点的路径最短,输出最短路径和目标点,如 ...

  9. 2015ACM/ICPC亚洲区沈阳站-重现赛 D - Pagodas

    题意:有\(n\)个数,开始给你两个数\(a\)和\(b\),每次找一个没出现过的数\(i\),要求满足\(i=j+k\)或\(i=j-k\),当某个人没有数可以选的时候判他输,问谁赢. 题解:对于\ ...

随机推荐

  1. 理解AngularJS中的依赖注入

    点击查看AngularJS系列目录 理解AngularJS中的依赖注入 AngularJS中的依赖注入非常的有用,它同时也是我们能够轻松对组件进行测试的关键所在.在本文中我们将会解释AngularJS ...

  2. String.getBytes(),源码之下,了无秘密

    @Deprecated public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) { if (srcBegin ...

  3. Linux - 设置/取消代理

    export http_proxy=118.210.42.251:44367 或: export https_proxy=118.210.42.251:44367   要取消该设置: unset ht ...

  4. js中的||与&&用法

    &&和||在JQuery源代码内尤为使用广泛,由网上找了些例子作为参考,对其用法研究了一下: &&: function a(){ alert("a" ...

  5. S2_OOP第一章

    面向对象设计的过程就是抽象的过程 步骤: 第一步:发现类 第二步:发现类的属性 第三步:发现类的方法 抽象是遵循的原则 属性和方法的设置是为了解决业务问题 关注主要属性和方法 如果没有必要,不增加额外 ...

  6. js-异步机制与同步机制

    Javascript的优势之一是其如何处理异步代码.异步代码会被放入一个事件队列,等到所有其他代码执行后才进行,而不会阻塞线程 1 理解异步代码: 1.1 JavaScript最基础的异步函数是set ...

  7. MVC调用部分视图PartialView

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Demo2 ...

  8. wpf 制作必输项的*标记

    直接引用帮助文档上的话吧,以免下次忘记! AdornedElementPlaceholder 类 .NET Framework 3.5   其他版本   此主题尚未评级 - 评价此主题   更新:20 ...

  9. 【机器学习实战】第5章 Logistic回归

    第5章 Logistic回归 Logistic 回归 概述 Logistic 回归虽然名字叫回归,但是它是用来做分类的.其主要思想是: 根据现有数据对分类边界线建立回归公式,以此进行分类. 须知概念 ...

  10. [转载]AI教师正来势汹汹,教师饭碗堪优

    (原文标题:开门,机器人老师来了) 一. 开门,机器人老师到了 国庆几天,河南刚刚上演一幕新科技的大戏: 计算机和人展开了为期四天的人机大战.这一次,对垒的双方不再是李世乭和阿尔法狗,而是教师和人工智 ...