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".
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.
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 = 2010; int n, num; char s[510][N]; void solve(int k) { if(k <= 0 || num == n - 1) return ; if(strstr(s[k], s[k - 1])) solve(k - 1); else { for(int i = n - 1 ; i >= num ; i--) { if(strstr(s[i], s[k - 1]) == 0) { num = i; break; } } solve(k - 1);//这一步递归是为了保证所找的值是最大的 } } int main() { int t, x = 0; scanf("%d", &t); while(t--) { num = -1; x++; scanf("%d", &n); for(int i = 0 ; i < n ; i++) scanf("%s", s[i]); solve(n - 1); if(num == -1) printf("Case #%d: -1 ", x); else printf("Case #%d: %d ", x, num + 1); } return 0; } /* 5 6 f d ba zabacd zabacd zabacdc */