Bazinga

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5510

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

HINT

题意

你需要找到一个最大的i使得,存在一个在他前面的字符串不是他的子串

题解:

就暴力匹配就好了,然后加一个剪枝,如果这个字符串是某个字符串的子串的话,就不用检查他了(讲道理的话,这个剪枝是没有用的,因为全部都不是子串的话,这个剪枝没有一点卵用。只是数据出水了而已……

正解应该是后缀自动机?AC自动机?

出题人的意思是只用检查相邻的两个字符串,好像很有道理的样子~

代码

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std; char s[][];
int vis[];
int main()
{
int t;scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
memset(vis,,sizeof(vis));
int n;scanf("%d",&n);
int flag = ;
for(int i=;i<=n;i++)
{
scanf("%s",s[i]);
for(int j=i-;j>=;j--)
{
if(vis[j])continue;
if(strstr(s[i],s[j])==NULL)flag=i;
else vis[j]=;
}
}
if(!flag)
printf("Case #%d: -1\n",cas);
else
printf("Case #%d: %d\n",cas,flag);
}
}

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 (kmp+dfs剪枝) 2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

    废话: 这道题很是花了我一番功夫.首先,我不会kmp算法,还专门学了一下这个算法.其次,即使会用kmp,但是如果暴力枚举的话,还是毫无疑问会爆掉.因此在dfs的基础上加上两次剪枝解决了这道题. 题意: ...

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

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

  5. hdu 5510 Bazinga(暴力)

    Problem Description Ladies and gentlemen, please sit up straight. Don't tilt your head. I'm serious. ...

  6. 【HDU 5510 Bazinga】字符串

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

  7. hdu 5510 Bazinga

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

  8. hdu 5510 Bazinga KMP+尺取法

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

  9. HDU 5510 Bazinga (字符串匹配)

    题目:传送门. 题意:t 组数据,每组 n 个串,对于第 i 个串如果存在 1 到 i-1 中的某个串不是 i 的子串,那么这个第 i 个串符合题意,求 i 的最大值. 题解:KMP,AC自动机也可以 ...

随机推荐

  1. 【转】SDP file

    SDP file Introduction The Session Description Protocol (SDP) is a format for describing the initiali ...

  2. android 官网处理图片 代码

    /** * 获取压缩后的图片 (官网大图片加载对应代码) * * @param res * @param resId * @param reqWidth * 所需图片压缩尺寸最小宽度 * @param ...

  3. 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(6)

    创建一个新文档(Creating a new document) 现在我们将尝试索引一些文档.对于我们的示例,让我们想象我们正在为我们的博客建立某种CMS.实体之一是博客的文章.使用JSON记法,在以 ...

  4. Hadoop 学习之 FAQ

    在Hadoop的学习与使用过程中同样如此.这里为大家分享Hadoop集群设置中经常出现的一些问题,以下为译文: 1.Hadoop集群可以运行的3个模式? 单机(本地)模式 伪分布式模式 全分布式模式 ...

  5. 【转】javascript-图片预加载技术

    1,脚本代码: /** * 图片头数据加载就绪事件 - 更快获取图片尺寸 * @version 2011.05.27 * @author TangBin * @see http://www.plane ...

  6. Multiclass Classification

    之前我们都是在Binary classification的基础上学习算法和知识. 如何使用Binary classification算法进行Multiclass classification呢? (一 ...

  7. [LeetCode] Ugly Number (A New Question Added Today)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  8. Tkinter教程之Text(2)篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811347 '''Tkinter教程之Text(2)篇''''''6.使用tag来指定文本的属 ...

  9. Genesis-3D开源游戏引擎简介!

    Genesis-3D由搜狐畅游公司超百人引擎研发团队历时数年耗费巨资自主研发,是国内外首款商业开源的3D游戏引擎平台.它包括跨平台渲染引擎.2D引擎.物理引擎.音效系统.粒子系统.动画系统.服务器引擎 ...

  10. 在WinForm编程中犯的一些错误

    1.一直以为,MouseClick事件在鼠标点击时发生,MouseDoubleClick事件在鼠标双击时发生.那么在单击鼠标时会调用MouseClick事件处理程序,双击鼠标时会调用MouseDoub ...