HDU 5510 Bazinga 暴力匹配加剪枝
Bazinga
Time Limit: 20 Sec
Memory Limit: 256 MB
題目連接
http://acm.hdu.edu.cn/showproblem.php?pid=5510
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
45
ab
abc
zabc
abcd
zabcd
4
you
lovinyou
aboutlovinyou
allaboutlovinyou
5
de
def
abcd
abcde
abcdef
3
a
ba
ccc
Sample Output
Case #1: 4Case #2: -1
Case #3: 4
Case #4: 3
HINT
?
題意
你需要找到一個(gè)最大的i使得,存在一個(gè)在他前面的字符串不是他的子串
題解:
就暴力匹配就好了,然后加一個(gè)剪枝,如果這個(gè)字符串是某個(gè)字符串的子串的話,就不用檢查他了(講道理的話,這個(gè)剪枝是沒有用的,因?yàn)槿慷疾皇亲哟脑?#xff0c;這個(gè)剪枝沒有一點(diǎn)卵用。只是數(shù)據(jù)出水了而已……
正解應(yīng)該是后綴自動(dòng)機(jī)?AC自動(dòng)機(jī)?
出題人的意思是只用檢查相鄰的兩個(gè)字符串,好像很有道理的樣子~
代碼
?
#include<iostream> #include<stdio.h> #include<cstring> using namespace std;char s[550][2005]; int vis[550]; int main() {int t;scanf("%d",&t);for(int cas=1;cas<=t;cas++){memset(vis,0,sizeof(vis));int n;scanf("%d",&n);int flag = 0;for(int i=1;i<=n;i++){scanf("%s",s[i]);for(int j=i-1;j>=1;j--){if(vis[j])continue;if(strstr(s[i],s[j])==NULL)flag=i;else vis[j]=1;}}if(!flag)printf("Case #%d: -1\n",cas);elseprintf("Case #%d: %d\n",cas,flag);} }?
轉(zhuǎn)載于:https://www.cnblogs.com/qscqesze/p/4929963.html
總結(jié)
以上是生活随笔為你收集整理的HDU 5510 Bazinga 暴力匹配加剪枝的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的iOS学习历程 - UISegmen
- 下一篇: BZOJ 1305 二分+网络流