HDU 5510:Bazinga(暴力KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=5510
Bazinga
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".
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.
题意:给出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)的更多相关文章
- hdu 5510 Bazinga(字符串kmp)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 5510 Bazinga 暴力匹配加剪枝
Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...
- Bazinga HDU 5510 Bazinga(双指针)
Bazinga HDU 5510 Bazinga(双指针) 题链 解法:对于串i来说,如果串i是不符合的,那么代表串i之前的字符串都是i的子串,那么我们求一个新的i(定义为ti),如果i是ti 的子串 ...
- hdu 5510 Bazinga (KMP+暴力标记)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 思路: 一开始直接用KMP莽了发,超时了,后面发现如果前面的字符串被后面的字符串包含,那么我们就 ...
- hdu 5510 Bazinga KMP+尺取法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:至多50组数据,每组数据至多500个字符串,每个字符串的长度最长为2000.问最大的下标( ...
- hdu 5510 Bazinga (kmp+dfs剪枝) 2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
废话: 这道题很是花了我一番功夫.首先,我不会kmp算法,还专门学了一下这个算法.其次,即使会用kmp,但是如果暴力枚举的话,还是毫无疑问会爆掉.因此在dfs的基础上加上两次剪枝解决了这道题. 题意: ...
- hdu 5510 Bazinga(暴力)
Problem Description Ladies and gentlemen, please sit up straight. Don't tilt your head. I'm serious. ...
- HDU 5510 Bazinga KMP
题意: 给\(n(1 \leq n \leq 500)\)个字符串,求一个最大的\(i\),使得存在一个\(S_{j}\)不是\(S_i\)的子串. 分析: 维护两个指针\(l,r\) 那么有两种情况 ...
- 【HDU 5510 Bazinga】字符串
2015沈阳区域赛现场赛第2题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:给定一个由字符串组成的序列,一共n个元素,每个元素是一个不 ...
- hdu 5510 Bazinga
http://acm.hdu.edu.cn/showproblem.php?pid=5510 Problem Description: Ladies and gentlemen, please sit ...
随机推荐
- x:Static
用途:访问代码中的变量等 后台定义一个变量 public partial class GetStaticFromBackgroundCode : Window { public static stri ...
- MVC基架生成的Create视图
@model MyMusicStore.Models.Album @{ ViewBag.Title = "Create"; } <h2>Create</h ...
- WPF Binding Path妙用
<Window x:Class="XamlTest.Window9" xmlns="http://schemas.microsoft.com/winf ...
- C# WebClient的使用
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- delphi备份恢复剪切板(使用了GlobalLock API函数和CopyMemory)
看了季世平老兄的C++代码后翻译过来的 unit clipbak; interface uses SysUtils, Classes, Clipbrd, Windows, Contnrs; type ...
- Html5 学习系列(四)文件操作API
原文:Html5 学习系列(四)文件操作API 引言 在之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很难进行跨平台.或者跨 ...
- Linux ssh密钥自动登录 专题
在开发中,经常需要从一台主机ssh登陆到另一台主机去,每次都需要输一次login/Password,很繁琐.使用密钥登陆就可以不用输入用户名和密码了 实现从主机A免密码登陆到主机B(即把主机A的pub ...
- 图像滤镜艺术--Toaster滤镜
原文:图像滤镜艺术--Toaster滤镜 根据Instagram CEO的说法,Toaster滤镜是Instagram所有滤镜中最复杂的滤镜,这个滤镜给人一种新奇的红色烘烤感,很能让人联想起这 ...
- Win8 Metro(C#)数字图像处理--2.36角点检测算法
原文:Win8 Metro(C#)数字图像处理--2.36角点检测算法 [函数名称] Harris角点检测函数 HarrisDetect(WriteableBitmap src, int ...
- Android零基础入门第1节:Android的前世今生
原文:Android零基础入门第1节:Android的前世今生 现在网上有很多各色Android资料了,但相对来说还是比较零散,Android覆盖的范围极广,最近刚好有机会全部拉通整理一遍,也保存起来 ...