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. Ubuntu更改 resolv.conf 重启失效

    更改Ubuntu的 resolv.conf的时候,重启的时候,经常又给重置了.google大法找到方法. sudo apt-get install resolvconf  原来Ubuntu的resol ...

  2. c# WebApi POST请求同时包含数据及其文件

    原因:创建.net WebApi的接口API.IIS作为服务端.安卓作为客户端发送json文件及其文件. Android端使用xUtils3.0实现文件上传 java代码: //要传递给服务器的jso ...

  3. PHP 一致性Hash

    一致性HASH 好久没有写文章了,最近忙着公司的事情,也一拖再拖.这篇一致性hash是很久之前就有的一篇算法,记录一下,这周写个基于该算法的Redis中间件. HASH算法的精髓就在于打散原本杂乱无序 ...

  4. Linux学习之“vfork函数”

    为什么使用vfork()? 希望父子进程执行不同的代码.例如: 网络服务程序中,父进程等待客户端的服务请求,当请求达到时,父进程调用fork,使子进程处理该次请求,而父进程继续等待下一个服务请求到达. ...

  5. 【Linux】查看物理CPU个数、核数、逻辑CPU个数

    ①物理cpu数:主板上实际插入的cpu数量,可以数不重复的 physical id 有几个(physical id) cat /proc/cpuinfo| grep "physical id ...

  6. SEED缓冲区溢出实验笔记

    缓冲区溢出实验(Linux 32位) 参考教程与材料:http://www.cis.syr.edu/~wedu/seed/Labs_12.04/Software/Buffer_Overflow/ (本 ...

  7. Lua中的协同程序 coroutine(转)

    Lua中的协程和多线程很相似,每一个协程有自己的堆栈,自己的局部变量,可以通过yield-resume实现在协程间的切换.不同之处是:Lua协程是非抢占式的多线程,必须手动在不同的协程间切换,且同一时 ...

  8. MySQL学习-SQL约束

    约束分类 主键:PRIMARY KEY用于唯一标识表中的一行,不可重复.e.g.:id INT(10) PRIMARY KEY 默认值:DEFAULT插入时,若没有指定该列的值,则为DEFAULT指定 ...

  9. LockWindowUpdate

     //锁住listview防止反复刷新              LockWindowUpdate(Self.lvsearch.Handle);    貌似不太行,多用几下就卡住了  那个函数几乎不用 ...

  10. 基于ASP.NET的新闻管理系统(三)代码展示

    5.1.1栏目部分 增加栏目(addLanMu.aspx): <html xmlns="http://www.w3.org/1999/xhtml"> <head  ...