题目链接

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. Nodejs·网络服务

    本章是从NodeJS拥有的模块角度,讲述了网络服务中的应用: net ----- > TCP dgram --> UDP http -----> HTTP https ----> ...

  2. 《Effective Java》—— 对于所有对象都通用的方法

    本节主要涉及Object中通用的一些方法,比如equals,hashCode,toString,clone,finalize等等 覆盖equals时请遵守通用约定 equals方法实现的等价关系: 自 ...

  3. jquery 插件开发

    一.$.extend()  这种方式用来定义一些辅助方法是比较方便的 $.extend({ sayHello:function(name){ console.log('Hello:'+name); } ...

  4. 3D touch在Unity3D中的使用

    0.开篇: 3D touch随着iOS9发布,它并不是一个单独的技术,而是可以分为pressure sensitivity.quick action以及peek&pop.在官方的介绍中提到可以 ...

  5. Grunt 安装与配置环境

    当时学习 Grunt 的时候,真是很头疼.分了两个时间段,学习了两次才硬啃下来,之后才能用在项目中.主要原因我认为是学习资料和文档上面写的太高端了.这类的文档或者资料有个显著特点,上来先简单介绍一下这 ...

  6. 绘制 ToggleButton --重写view

    package com.example.compoundbuttonview.view; import com.example.compoundbuttonview.R; import android ...

  7. c#基础之长度可变类型相同的参数列表

    为了简化编码,c#提供了一个特殊的关键字params,允许在调用方法时提供数量可变的实参,而不是由方法实现固定好的形参数量.先看代码吧. using System; using System.Linq ...

  8. Linux - 变量

    printenv - print all or part of environment 显示所有变量:print 显示某个变量:print <variable name> 或者 echo ...

  9. 可视化工具solo show-----Prefuse自带例子GraphView讲解

    2014.10.15日以来的一个月,挤破了头.跑断了腿.伤透了心.吃够了全国最大餐饮连锁店——沙县小吃.其中酸甜苦辣,绝不是三言两语能够说得清道的明的.校招的兄弟姐妹们,你们懂得…… 体会最深的一句话 ...

  10. web前端学习笔记(CSS盒子的定位)

    相对定位 使用相对定位的盒子的位置常以标准流的排版方式为基础,然后使盒子相对于它在原本的标准位置偏移指定的距离.相对定位的盒子仍在标准流中,它后面的盒子仍以标准流方式对待它.      使用relat ...