
http://acm.hdu.edu.cn/showproblem.php?pid=5510
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".
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.
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
*/