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. R语言实战读书笔记(四)基本数据管理

    4.2 创建新变量 几个运算符: ^或**:求幂 x%%y:求余 x%/%y:整数除 4.3 变量的重编码 with(): within():可以修改数据框 4.4 变量重命名 包reshape中有个 ...

  2. Spring 实践 -拾遗

    Spring 实践 标签: Java与设计模式 Junit集成 前面多次用到@RunWith与@ContextConfiguration,在测试类添加这两个注解,程序就会自动加载Spring配置并初始 ...

  3. css各浏览器的兼容性写法

    各浏览器下的兼容性写法 老版Chrome     -webkit-xxx FF                  -moz-xxx IE9                 -ms-xxx opera ...

  4. ADO与ADO.NET的区别与介绍

    1. ADO与ADO.NET简介ADO与ADO.NET既有相似也有区别,他们都能够编写对数据库服务器中的数据进行访问和操作的应用程序,并且易于使用.高速度.低内存支出和占用磁盘空间较少,支持用于建立基 ...

  5. C#序列化XML至对象

    内容来源: http://www.cnblogs.com/fish-li/archive/2013/05/05/3061816.html#_label0 自己做的Demo下载地址:Demo

  6. 代码记录:使用Aforge.net让视频图像反转180度

    private void CameraConn() { videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedI ...

  7. jupyterhub

    pkill jupyterhub #激活python环境 pyenv activate jupyterhub #启动jupyterhub /fly/start_jupyterhub.sh cd ~/r ...

  8. 20160127.CCPP体系详解(0006天)

    程序片段(01):msg.c 内容概要:线程概念 #include <stdio.h> #include <stdlib.h> #include <Windows.h&g ...

  9. 【转】Eclipse常用快捷键

    原文网址:http://denver.blog.51cto.com/272871/52219 编辑相关快捷键 1.[Alt+/] 为用户提供内容的辅助. 2.[Ctrl+O] 显示类中方法和属性的大纲 ...

  10. UE删除空行