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. WPF利用VisualTreeHelper遍历寻找对象的子级对象或者父级对象

    原文:WPF利用VisualTreeHelper遍历寻找对象的子级对象或者父级对象 简介 本文将完整叙述我利用VisualTreeHelper实现题述功能的全部过程,想直接看函数实现的朋友可以跳到函数 ...

  2. WCF配置文件详解

    今天来看看WCF的配置方法. 上图整理了服务配置过程中所用到的基本的元素,大致的步骤主要是首先要在调用服务的程序集中添加服务的一个引用,然后添加一个service并指定服务的名称.终结点,如果添加了b ...

  3. github中README.md文件写法解析,git指令速查表

    http://blog.csdn.net/u012234115/article/details/41778701 http://blog.csdn.net/u012234115/article/det ...

  4. 在 __CC_ARM 编译器环境下,使用$Sub$$ 与 $Super$$ 的“补丁”功能

    $Sub$$ 与 $Super$$ 的“补丁”功能(详见 ARM® Compiler v5.06 for µVision® armlink User Guide): 这是一种特殊模式:用于有一个已经存 ...

  5. Image Captioning代码复现

    Image caption generation: https://github.com/eladhoffer/captionGen Simple encoder-decoder image capt ...

  6. Win8Metro(C#)数字图像处理--2.18图像平移变换

    原文:Win8Metro(C#)数字图像处理--2.18图像平移变换  [函数名称] 图像平移变换函数TranslationProcess(WriteableBitmap src,int x,in ...

  7. 程序定义了多个入口点。使用 /main (指定包含入口点的类型)进行编译

    原文:请使用/main进行编译,以指定包含入口点类型 在使用VS工具初学C#的时候需要不停的写小程序,觉得每次都新建项目太过麻烦,所以试着把程序写在一个项目下面,结果编译的时候出错了,因为我每个小程序 ...

  8. 笔记:Advanced Installer 打包Web应用

    原文:笔记:Advanced Installer 打包Web应用 公司要做一款增值税小产品,区别于ACME,本产品核心只有销项部分,面对的客户群是小企业,单税盒单开票机..... 我要做的主要有以下几 ...

  9. 如何用C++操作无线网卡开启共享热点WiFi?

    首先需要笔记本具备AP热点功能,记得写好的程序必须用管理员身份运行. 准备工作需要先做好 //查看是否支持的承载网络 netsh wlan show drivers //设置网络模式为allow ne ...

  10. 有效地查找SAP增强点

    找SAP增强点一直都是SAP开发的重点难点,增强开发的代码一般不会很多,但是需要花费比较多的时间在查找增强点上 网上也流传了很多查找SAP增强的方法: 1.利用TCODE寻找增强 2.利用系统函数寻找 ...