http://acm.hdu.edu.cn/showproblem.php?pid=5510

Bazinga

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

题意:给出n个串,求出最大的i使得有一个j(1<=j<i)不是i的子串。

思路:比赛的时候由于没剩下多少时间,写的太暴力了TLE,后面自己写了一个,发现别人写的也很暴力,还有用strstr过的,速度普遍比用KMP的快。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 2010
#define N 510
int nxt[M];
char s[N][M]; void make_next(char *s)
{
memset(nxt, , sizeof(nxt));
int j = -, i = ;
nxt[] = -;
int len = strlen(s);
while(i < len) {
if(j == - || s[i] == s[j]) {
i++; j++;
nxt[i] = j;
} else {
j = nxt[j];
}
}
} int kmp(char *s, char *str)
{
make_next(s);
int l = strlen(s), len = strlen(str);
int i = , j = ;
while(i < len && j < l) {
if(j == - || str[i] == s[j]) {
i++; j++;
if(j >= l) return true;
} else {
j = nxt[j];
}
}
return false;
} int main()
{
int t;
int cas = ;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++)
scanf("%s", s[i]); int tmp = -;
for(int i = n; i > ; i--){
if(!kmp(s[i-], s[i])) {
tmp = i;
break;
}
}
// printf("TMP : %d\n", tmp);
int ans = tmp;
for(int i = tmp - ; i > ; i--) {
while(!kmp(s[i], s[ans+]) && ans < n) {
ans++;
}
} printf("Case #%d: ", ++cas);
if(tmp != -)
printf("%d\n", ans);
else
printf("-1\n");
}
return ;
}

HDU 5510:Bazinga(暴力KMP)的更多相关文章

  1. hdu 5510 Bazinga(字符串kmp)

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

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

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

  3. Bazinga HDU 5510 Bazinga(双指针)

    Bazinga HDU 5510 Bazinga(双指针) 题链 解法:对于串i来说,如果串i是不符合的,那么代表串i之前的字符串都是i的子串,那么我们求一个新的i(定义为ti),如果i是ti 的子串 ...

  4. hdu 5510 Bazinga (KMP+暴力标记)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 思路: 一开始直接用KMP莽了发,超时了,后面发现如果前面的字符串被后面的字符串包含,那么我们就 ...

  5. hdu 5510 Bazinga KMP+尺取法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:至多50组数据,每组数据至多500个字符串,每个字符串的长度最长为2000.问最大的下标( ...

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

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

  7. hdu 5510 Bazinga(暴力)

    Problem Description Ladies and gentlemen, please sit up straight. Don't tilt your head. I'm serious. ...

  8. HDU 5510 Bazinga KMP

    题意: 给\(n(1 \leq n \leq 500)\)个字符串,求一个最大的\(i\),使得存在一个\(S_{j}\)不是\(S_i\)的子串. 分析: 维护两个指针\(l,r\) 那么有两种情况 ...

  9. 【HDU 5510 Bazinga】字符串

    2015沈阳区域赛现场赛第2题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:给定一个由字符串组成的序列,一共n个元素,每个元素是一个不 ...

  10. hdu 5510 Bazinga

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

随机推荐

  1. UVA - 825Walking on the Safe Side(dp)

    id=19217">称号: UVA - 825Walking on the Safe Side(dp) 题目大意:给出一个n * m的矩阵.起点是1 * 1,终点是n * m.这个矩阵 ...

  2. wpf采用Xps实现文档显示、套打功能

    原文:wpf采用Xps实现文档显示.套打功能 近期的一个项目需对数据进行套打,用户要求现场不允许安装office.页面预览显示必须要与文档完全一致,xps文档来对数据进行处理.Wpf的Document ...

  3. 静态资源(StaticResource)和动态资源(DynamicResource)

    一.文章概述 本演示介绍了WPF的静态资源和动态资源的基本使用,并对两者做了简单的比较. 二.定义并使用资源 <Window x:Class="Demo010.MainWindow&q ...

  4. ES6中的Promise详解

    Promise 在 JavaScript 中很早就有各种的开源实现,ES6 将其纳入了官方标准,提供了原生 api 支持,使用更加便捷. 定义 Promise 是一个对象,它用来标识 JavaScri ...

  5. Linux参数调优

    一.系统参数调优 打开文件 /etc/sysctl.conf ############ # 一般服务器调整 # ############ #最大连接数 net.core.somaxconn = 327 ...

  6. BGP路由的手动汇总

    网络拓扑 XRV1 ========================================================== !hostname XRV1!interface Loopba ...

  7. asp.net ToString() 格式化字符串

    c# ToString() 格式化字符串  格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化.我们使用格式字符串指定格式.格式字符串采用以下形式:Axx,其中 A 为格式 ...

  8. java基础之super关键字

    一.在java里面,对于super关键字通常有两种用法: 1. 用在子类的构造方法里(初始化用),主要是调用父类的默认构造方法,如果父类有不止一个构造方法,可以通过super指定具体的构造函数,比如 ...

  9. 比较DirectX和OpenGL的区别(比较详细)

    OpenGL是个专业的3D程序接口,是一个功能强大,调用方便的底层3D图形库.OpenGL的前身是SGI公司为其图形工作站开发的IRIS GL.IRIS GL是一个工业标准的3D图形软件接口,功能虽然 ...

  10. Understand the Qt containers(有对应表)

    Container classes are one of the cornerstones of object-oriented programming, invaluable tools that ...