hdu5510 Bazinga
来源:互联网 发布:java软件安装教程 编辑:程序博客网 时间:2024/06/06 13:07
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 532 Accepted Submission(s): 220
Total Submission(s): 532 Accepted Submission(s): 220
Problem Description
Ladies and gentlemen, please sit up straight.
Don't tilt your head. I'm serious.
Forn 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 stringSi 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
A substring of a string
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 integern (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 than2000 letters.
For each test case, the first line is the positive integer
All strings are given in lower-case letters and strings are no longer than
Output
For each test case, output the largest label you get. If it does not exist, output −1 .
Sample Input
45ababczabcabcdzabcd4youlovinyouaboutlovinyouallaboutlovinyou5dedefabcdabcdeabcdef3abaccc
Sample Output
Case #1: 4Case #2: -1Case #3: 4Case #4: 3
这是一道双指针kmp题,因为如果A是B的子串,那么后面判断的时候A就可以省略不判断,因为如果A不是其子串,B肯定不是其子串。
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;typedef long long ll;char s[505][2005],nextt[2005];int pd(char *s1,char *s2){ int i,j,len1,len2; len1=strlen(s1); len2=strlen(s2); i=0;j=-1; memset(nextt,-1,sizeof(nextt)); while(i<len2){ if(j==-1 || s2[i]==s2[j]){ i++; j++; nextt[i]=j; } else j=nextt[j]; } i=0;j=0; while(i<len1 && j<len2){ if(j==-1 || s1[i]==s2[j]){ i++; j++; } else j=nextt[j]; } if(j>=len2) return 1; else return 0;}int main(){ int T,n,m,i,j,ans,num,cas=0; scanf("%d",&T); while(T--) { scanf("%d",&n); ans=-1; for(i=1;i<=n;i++){ scanf("%s",s[i]); } j=1; for(i=2;i<=n;i++){ //这里用两个指针,i指当前位置,j指遍历到哪个串 while(j<i && pd(s[i],s[j]) )j++; if(j<i)ans=i; } cas++; printf("Case #%d: %d\n",cas,ans); } return 0;}
0 0
- hdu5510 Bazinga
- HDU5510 Bazinga
- HDU5510 Bazinga KMP+剪枝
- hdu5510 Bazinga 暴力+剪枝
- HDU5510 Bazinga (strstr())
- 【KMP+暴力】Bazinga hdu5510
- HDU5510 - Bazinga (kmp+暴力)
- HDU5510 Bazinga 暴力strstr+剪枝
- hdu5510-沈阳区域赛 Bazinga【KMP】
- hdu5510
- Bazinga
- 【HDU5510 2015沈阳赛区B】【KMP or strstr for循环剪枝】Bazinga 循环处思维灵活转化 时间复杂度均摊思想
- Bazinga HDU
- hdu5510 (KMP+剪枝)
- hdu5510 kmp+二分
- HDU5510(KMP)
- hdu5510(并查集+KMP)
- HDU5510-思维-字符串匹配剪枝
- iOS 开发懒加载
- python里面如何获的日期值
- FFTW3安装与库环境变量设置(路径设置)
- 《leetCode》:3Sum Closest
- HTML学习12:其他常见标签之头标签
- hdu5510 Bazinga
- undefined reference to __android_log_print
- StringBuffer
- leetcode之Binary Search Tree Iterator
- UML第十二集(14)之活动图
- 第10周-Person类
- 动态规划(2)
- href中jstl的输入
- Python入门——字典的浅拷贝与深拷贝