题目链接

http://acm.hdu.edu.cn/search.php?action=listproblem

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:  5910 5909 5908 5907 5906 
 
题意:输入n 然后输入n个字符串,求最大的i 要求1~i-1中有一个串不是i的子串?
 
思路:分析复杂度可知这n个字符串比较次数只能是O(n) 那么发现可以用指针模拟的办法解决。具体做法:定义l和r 如果l是r的子串,那么l++ 继续判断l是否是r的子串,否则ans=r, 为什么这样呢?  如果l是r的子串那么l++  由它可知1~l-1 这些串一定是l~r这些串中一些串的子串,那么1~l-1这些串不必再和r后面的串进行比较;
 
代码如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <queue> using namespace std;
typedef long long LL;
char s[][];
int nex[]; void makeNext(char p[])
{
int q,k;
int m=strlen(p);
nex[]=;
for(q=,k=; q<m; ++q)
{
while(k>&&p[q]!=p[k])
k=nex[k-];
if(p[q]==p[k]) k++;
nex[q]=k;
}
}
int calc(int x,int y)
{
makeNext(s[x]);
int l=strlen(s[x]);
int len=strlen(s[y]);
int q,k;
for(q=,k=; q<len; q++)
{
while(k>&&s[y][q]!=s[x][k])
k=nex[k-];
if(s[y][q]==s[x][k]) k++;
if(k>=l) return ;
}
return ;
} int main()
{
int T,Case=;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%s",s[i]);
int l = , r = , ans = -;
while(r <= n)
{
while(l < r)
{
if(calc(l,r)) l++;
else { ans=r; break; }
}
r++;
}
printf("Case #%d: %d\n",Case++,ans);
}
return ;
}

HDU 5510---Bazinga(指针模拟)的更多相关文章

  1. hdu 5510 Bazinga(字符串kmp)

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

  2. Bazinga HDU 5510 Bazinga(双指针)

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

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

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

  4. hdu 5510 Bazinga

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

  5. hdu 5510 Bazinga KMP+尺取法

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

  6. 【HDU 5510 Bazinga】字符串

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

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

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

  8. HDU 5510 Bazinga (2015沈阳现场赛,子串判断)

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

  9. HDU 5510 Bazinga KMP

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

随机推荐

  1. H5常用代码:适配方案2

    前面的通过视口做适配的方案由于安卓低版本原生浏览器的存在,在许多场合不尽如人意,会在低版本安卓上出现,不缩放,手动缩放未禁止的问题. 于是出现了第二种适配方案,既然通过视口缩放可以兼容,那为什么不直接 ...

  2. 修复发生“由于数据移动,未能继续以 NOLOCK 方式扫描”错误的数据库

    最近在系统运行中发现了一个错误,错误信息如下: 错误信息:查询A201412C20568单证信息错误 ---> System.Data.OleDb.OleDbException: 由于数据移动, ...

  3. javascript_core_10之继承与数组API

    1.现有两对象间的继承:Object.setPrototypeOf(child,father): 2.基于现有父对象创建子对象:var child=Object.create(father,{新属性} ...

  4. 学习bootstrap遇到的问题--001 关于bootstrap中类.disabled不禁用默认行为

    自学bootstrap遇到的疑惑篇: 按钮状态--禁用 在Bootstrap框架中,要禁用按钮有两种实现方式: 方法1:在标签中添加disabled属性 方法2:在元素标签中添加类名"dis ...

  5. ligerUI Tree 实例 代码

    http://www.oschina.net/code/snippet_1762525_47819#68813

  6. .NET面试题解析(05)-常量、字段、属性、特性与委托

      系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 弱小和无知不是生存的障碍,傲慢才是!——<三体> 常见面试题目: 1. const和reado ...

  7. hdu 1241 Oil Deposits (一次dfs搞定有某有)

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

  8. Razor基本语法

    前言: Razor引擎的核心是识别@符号及尖括号:    1.<...></...>    2.<.../>    [之所以说是"尖括号"而非& ...

  9. nodejs支持ssi实现include shtml页面

    express 对于include的代码默认不处理,直接输出,没办法执行include的内容,但可以通过Nginx实现. 1. 配置nginx设置开启ssi模式. server { … ssi on; ...

  10. Java多线程系列--“JUC集合”06之 ConcurrentSkipListSet

    概要 本章对Java.util.concurrent包中的ConcurrentSkipListSet类进行详细的介绍.内容包括:ConcurrentSkipListSet介绍ConcurrentSki ...