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

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个串,编号从1到n让找一个串i看它上面的串中是否存在一个串不是这个串i的子串,如果不存在输出-1,否则输出i(使编号i尽可能大)
 
分析:
 
通过题意可以知道,i串的子串必须在i串的上面找,那么我们可以两个两个的比较
如这个例子:

5
6
f    (1)
d   (2)
ba  (3)
zabacd   (4)
zabacd   (5)
zabacdc  (6)

它们的编号分别为1、2、3、4、5、6,那么我们从i最高的地方开始找

(6)和(5)比较发现(5)是(6)的子串,则比较(5)和(4),发现(4)是(5)的子串,则比较(4)和(3)...<既然(5)是(6)的子串,那么只有是

(5)的子串,那么就一定是(6)的子串,这样两个两个的比较,如果一个是另一个的子串就不需要再管,继续找,直到找到不是子串(或所以串找完)为止>

比较到(3)和(2)时发现(2)不是(3)的子串,那么此时就需要看(2)是不是(4)到(6)的子串了,发现(2)是(4)到(6)的子串,那么我们此时得

到的结果i是3,但3到底是不是我们我要得到的最大值呢,我们继续从(2)开始,比较(2)和(1),发现(1)也不是(2)的子串, 那么就看(1)是不是

(3)<这里为什么是3而不是1呢?因为我们之前已经的到一个结果3,那么我们下面再找的i只要>=3就可以了, 所以比较就只需查找从6到3就可以了,>到

(6)的子串,发现(1)不是(6)的子串,的到i的结果是6,是不是比之前的到的结果3大呢,所以最终结果为6(这些过程可以用一个递归函数来搞定了)

(讲的是不是有点不太清楚呢 ^_^!....对着这个例子调试一遍就OK了)

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std; const int N = ; int n, num;
char s[][N]; void solve(int k)
{
if(k <= || num == n - )
return ;
if(strstr(s[k], s[k - ]))
solve(k - );
else
{
for(int i = n - ; i >= num ; i--)
{
if(strstr(s[i], s[k - ]) == )
{
num = i;
break;
}
}
solve(k - );//这一步递归是为了保证所找的值是最大的
}
} int main()
{
int t, x = ;
scanf("%d", &t);
while(t--)
{
num = -;
x++;
scanf("%d", &n);
for(int i = ; i < n ; i++)
scanf("%s", s[i]);
solve(n - );
if(num == -)
printf("Case #%d: -1\n", x);
else
printf("Case #%d: %d\n", x, num + );
}
return ;
}
/*
5
6
f
d
ba
zabacd
zabacd
zabacdc
*/

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 KMP+尺取法

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

  5. 【HDU 5510 Bazinga】字符串

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

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

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

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

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

  8. TTTTTTTTTTTTTTTT hdu 5510 Bazinga 字符串+哈希

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

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

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

随机推荐

  1. Qt之QLCDNumber

    简述 QLCDNumber控件用于显示一个LCD数字. 它可以显示几乎任意大小的数字.可以显示十进制.十六进制.八进制或二进制数.很容易使用display()槽连接到数据源,这个槽可以被任何五个参数类 ...

  2. Excel文件操作方式比较

    C++读取Excel的XLS文件的方法有很多,但是也许就是因为方法太多,大家在选择的时候会很疑惑. 由于前两天要做导表工具,比较了常用的方法,总结一下写个短文, 1.OLE的方式 这个大约是最常用的方 ...

  3. JavaScript——new Date().getMonth()

    new Date().getMonth(); 说明:参照现在的月份,getMonth()返回的是0-11的数字,也就是说0=1月,1=2月……11=12月 当前的月份就是,new Date().get ...

  4. Android Terminal telnet windows

    /******************************************************************************************** * Andr ...

  5. maven整合s2sh截图

  6. 如何使用java中的对象

    使用java中的对象,分2步: 1.创建一个对象: 2.使用对象的属性和方法. 见下面的示例代码: package com.imooc; //1.定义一个类 public class Telphone ...

  7. android部分手机onclick事件触发2次

    var t1 = null; function btnSave() { if (t1 == null) { t1 = new Date().getTime(); } else { var t2 = n ...

  8. 深入学习Struts2

    本部分主要介绍struts.xml的常用配置. 1.1.    包配置: Struts2框架中核心组件就是Action.拦截器等,Struts2框架使用包来管理Action和拦截器等.每个包就是多个A ...

  9. ipad iphone 开发的应用,加一个启动预览图片

    如果你想在你的ipad,iphone应用启动是不显示黑屏幕,而是用一个图片代替的话,你只需要吧一张名为 Default.png 的图片加到项目里就行了. 很简单吧,新手们记得去这样美化下你的应用吗,很 ...

  10. Linux 安装JDK7 遇到的问题

    Error occurred during initialization of VMjava/lang/NoClassDefFoundError: java/lang/Object 如出现 unpac ...