HDU - 5510 Bazinga
阿新 • • 發佈:2018-10-04
fir n) can %s all 代碼 -m panel led
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 S.
All strings are given in lower-case letters and strings are no longer than 2000 letters.OutputFor each test case, output the largest label you get. If it does not exist, output ?1.Sample Input
Sample Output
Ladies and gentlemen, please sit up straight.
Don‘t tilt your head. I‘m serious.
For n given strings S, 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 S is not a substring of S.
A substring of a string S is another string that occurs
in S
For each test case, the first line is the positive integer n (1≤n≤500) and in the following
All strings are given in lower-case letters and strings are no longer than 2000 letters.OutputFor 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
Case #1: 4 Case #2: -1 Case #3: 4 Case #4: 3
思路:
暑訓的時候曾經寫過這道題,不過我竟然忘了正解,實際上這題還是比較暴力的,由於題目要求的只是哪一個存在就行啦,所以在層層嵌套的情況下,不需要完全直接掃一編。
於是,我們首先處理出,相鄰的有哪些是滿足字串的關系的,然後再做處理就行啦。
代碼
#include<iostream> #include<cstring> using namespace std; char a[505][2048]; int num[600]; int main() { int T; scanf("%d",&T); int Ca = 0; while(T--){ Ca++; int n; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%s",a[i]); } int ans=0; for(int i=1;i<n;i++){ num[i]=strstr(a[i+1],a[i])?1:0; } for(int i=n;i>=1;i--){ for(int j=1;j<i;j++){ if(num[j]){continue;} if(!strstr(a[i],a[j])){ ans=i; break; } } if(ans){break;} } printf("Case #%d: ",Ca); if(ans){printf("%d\n",ans);} else printf("-1\n"); } }
HDU - 5510 Bazinga