日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 5510 Bazinga 暴力匹配加剪枝

發(fā)布時(shí)間:2024/7/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 5510 Bazinga 暴力匹配加剪枝 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

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

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。