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. Less tips:声明变量之前可以引用变量!

    Less中的variable可以在使用之后才被声明,这一特性对于希望覆盖前期声明的(比如bootstrap等第三方library的variable)变量,从而优雅地 使用你希望的效果提供了便利. 比如 ...

  2. UVa 1479 (Treap 名次树) Graph and Queries

    这题写起来真累.. 名次树就是多了一个附加信息记录以该节点为根的树的总结点的个数,由于BST的性质再根据这个附加信息,我们可以很容易找到这棵树中第k大的值是多少. 所以在这道题中用一棵名次树来维护一个 ...

  3. Mac设置截图保存位置

    补充: killall 用来杀死指定名字的进程 defaults 可以对一些系统属性进行read,write,delete操作 下面举几个常用的例子: 1.显示隐藏文件 defaults write ...

  4. 【转】Xcode6 模拟器路径

    原文网址:http://www.cocoachina.com/bbs/read.php?tid-231024.html Xcode6发布后,出现了很多的变动,功能性的变动,在这里不进行过多的赘述,在W ...

  5. C# Math.Round中国式的四舍五入法

    double dou = 1.255; //这种是错误的 double dou_result = Math.Round(dou, 2); //结果: 1.25 dou_result = Math.Ro ...

  6. Codeforces Round #218 (Div. 2) C题

    C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. c#中const、static、readonly的区别

    1. const与readonly const ,其修饰的字段只能在自身声明时初始化. Readonly 是只读变量,属于运行时变量,可以在类初始化的时候改变它的值.该类型的字段,可以在声明或构造函数 ...

  8. Web通信之:长轮询(long-polling)(转)

    Web通信之:长轮询(long-polling) “轮询”是个耐人寻味的词,第一次看到它的时候我就直接理解为“轮流查询”了.但是看到了英文才知道这个是网络通信专业的术语.轮询,其实就是一群人在排队买东 ...

  9. opengl奔溃问题

    按照网上的教程编译成功,当时运行时老是奔溃(不弹出任何提示,窗口变灰色).遂更新了ATI的显卡驱动果然可以运行了,哈哈 http://support.amd.com/en-us/download/de ...

  10. Zabbix监控Linux磁盘I/O

    东西都上传到这里了: https://github.com/RexKang/Zabbix/tree/master/OS/Linux-disk-discovery   需要用到的东西: Zabbix的L ...