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. JS中关于数组的内容

      前  言 LIUWE 在网站制作过程中,数组可以说是起着举足轻重的地位.今天就给大家介绍一下数组的一些相关内容.例如:如何声明一个数组和在网站制作过程中我们常用的一些数组的方法.介绍的不好还请多多 ...

  2. 计算机基础--Java中int char byte的关系

    计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...

  3. 使用paramiko远程执行

    写部署脚本时,难免涉及到一些远程执行命令或者传输文件. 之前一直使用sh库,调用sh.ssh远程执行一些命令,sh.scp传输文件,但是实际使用中还是比较麻烦的,光是模拟用户登陆这一点,还需要单独定义 ...

  4. SiganlR 系列之概述

    简介 SignalR 是微软的 http 长连接(以下简称长连接)框架,它的出现为我们提供了一套行之有效的实时通信的解决方案. 背景 在http 1.0 时代,preRequest 都会建立新的tcp ...

  5. Print Article hdu 3507 一道斜率优化DP 表示是基础题,但对我来说很难

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  6. 支持向量机(Support Vector Machine)-----SVM之SMO算法(转)

    此文转自两篇博文 有修改 序列最小优化算法(英语:Sequential minimal optimization, SMO)是一种用于解决支持向量机训练过程中所产生优化问题的算法.SMO由微软研究院的 ...

  7. Windows中的硬链接和软链接(hard link 和 Symbolic link)

    先来了解一下Linux中的硬链接和软链接: Linux中的硬链接和软链接 Windows中的硬链接和软链接: 硬链接 从Windows NT4开始,NTFS文件系统引入了HardLink这个概念,它让 ...

  8. c# datetime与 timeStamp(unix时间戳) 互相转换

    /// <summary> /// Unix时间戳转为C#格式时间 /// </summary> /// <param name="timeStamp" ...

  9. 从字符串获得MAC地址的方法

    今日有感于编程水平下降,特意练习一下,根据MAC地址字符串,获取MAC地址的2种方法. #include <stdio.h> void func1(char *str){ unsigned ...

  10. DevOps之唠叨话

    唠叨话 教学:人类培养态度.传授知识.训练技能的活动. 教学手册(Teaching Manual) 教学形式:教材与课程,师生(一对一.一对多).教学内容:系统框架和问答机制,结构(前言.目录.正文. ...