1.POJ 3450 Coporate Identity
这两题的解法都是枚举子串,然后匹配,像这种题目以后可以不用KMP来做,直接字符串自带的strstr函数搞定,如果字符串未出现,该函数返回NULL。
下面贴出其比较。
代码:(KMP版)(1360ms 888KB)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 4007 char ans[203],str[203]; char ss[N][203],tt[203]; int next[203]; void getnext(char *ss) { int m = strlen(ss); next[0] = -1; int i = 0,j = -1; while(i<m) { if(j == -1 || ss[i] == ss[j]) next[++i] = ++j; else j = next[j]; } } int kmp(char *ss,char *tt) { int n = strlen(ss); int m = strlen(tt); getnext(tt); int i = -1,j = -1; while(i<n && j<m) { if(j == -1 || ss[i] == tt[j]) i++,j++; else j = next[j]; } if(j == m) return 1; return 0; } int main() { int n,i,len,j,k,lengh; while(scanf("%d",&n)!=EOF && n) { for(i=0;i<n;i++) scanf("%s",ss[i]); lengh = strlen(ss[0]); ans[0] = '