第一种形式
数据结构实验之串一:KMP简单应用
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给定两个字符串string1和string2,判断string2是否为string1的子串。
Input
输入包含多组数据,每组测试数据包含两行,第一行代表string1(长度小于1000000),第二行代表string2(长度小于1000000),string1和string2中保证不出现空格。
Output
对于每组输入数据,若string2是string1的子串,则输出string2在string1中的位置,若不是,输出-1。
Sample Input
abc a 123456 45 abc ddd
Sample Output
1 4 -1
Hint
Source
cjx
代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str1[1000001] ;
char str2[1000001] ;
int next[1000001] ;
void set()
{
int j = -1;
int i = 0 ;
int len =strlen(str2) ;
next[0] = -1 ;
while(i<len)
{
if(j == -1 || str2[i] == str2[j])
{
i++ ;
j++ ;
next[i] =j ;
}
else j = next[j] ;
}
}
int find()
{
int len1 , len2 ;
int i ,j ;
len1 = strlen(str1) ;
len2 = strlen(str2) ;
i = 0 ; j = 0 ;
while(i<len1&&j<len2)
{
if(str1[i] == str2[j] || j == -1)
{
i++ ;
j++ ;
}
else j = next[j] ;
}
if(j == len2 )
{
return i-len2+1 ;
}
else return -1 ;
}
int main()
{
int pos ;
while(~scanf("%s %s",str1,str2))
{
set() ; //构建next数组
pos = find() ; //返回子串在主串的位置
printf("%d\n",pos) ;
}
return 0 ;
}
第二种形式(对next数组的优化)
题同上
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char a[1000001] ;
char b[1000001] ;
int next[1000001] ;
void set()
{
int len = strlen(b) ;
int i = 0 ;
next[0] = -1 ;
int j = -1 ;
while(i<len)
{
if(j == -1||b[i] == b[j])
{
i++ ;
j++ ;
if(b[i]!=b[j])
{
next[i] = j ;
}
else next[i] = next[j] ;
}
else j = next[j] ;
}
}
int kmp()
{
int l1 = strlen(a) ;
int l2 = strlen(b) ;
int i = 0 ;
int j = 0 ;
while(i<l1&&j<l2)
{
if(j == -1||a[i] == b[j])
{
i++ ;
j++ ;
}
else j = next[j] ;
}
if(j == l2)
return i - l2 +1 ;
else return -1 ;
}
int main()
{
while(~scanf("%s",a))
{
scanf("%s",b) ;
set() ; //创建next数组
int d = kmp() ;
printf("%d\n",d) ;
}
return 0 ;
}
第三种形式(最大长度表)
Good Luck!
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
我们都知道,前缀就是一个单词的前几个字母(长度小于单词长度);后缀就是一个单词的后几个字母(长度小于单词长度)。例如:Hello,{H,He,Hel,Hell}都是Hello的前缀,{ello,llo,lo,o}都是Hello的后缀。现在,给你一个字符串String,你的任务是找出一个字串s,s既是String的前缀,又是String的后缀,并且s也出现在String的中间部分(既不是前缀,也不是后缀),s的长度越长越好。
Input
输入一个N,接下来N行,每行一个字符串String,String长度len( 1 <= len <= 1000000)。
Output
输出只有一行,如果有符合条件的s,输出长度最长的s,如果没有,输出“Bad Luck!”(不含引号)。
Sample Input
3 abcabcabcabcabc papapapap aaabaaaabab
Sample Output
abcabcabc papap Bad Luck!
Hint
Source
GLSilence
代码如下:
import java.util.Scanner;
public class Main{
static int next[] = new int[1000001] ;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in) ;
int t= sc.nextInt() ;
sc.nextLine();
while(t-->0)
{
String str = sc.nextLine() ;
setNext(str) ;
int len = str.length() ;
if(len<3||next[len-1]==-1)
{
System.out.println("Bad Luck!");
}
else
{
int l = len-1;
boolean tag = false ;
while(next[l]!=-1)
{
if(str.indexOf(str.substring(0,next[l]+1),1)!=len-1-next[l])
{
System.out.println(str.substring(0,next[l]+1));
tag = true ;
break ;
}
l = next[l] ;
}
if(!tag)
System.out.println("Bad Luck!");
}
}
sc.close() ;
}
private static void setNext(String str) {
int j ;
j = -1 ;
next[0] = -1 ;
for(int i= 1 ;i<str.length() ;i++)
{
while(j!=-1&&str.charAt(i)!=str.charAt(j+1))
j = next[j] ;
if(str.charAt(i) == str.charAt(j+1))
j++ ;
next[i] = j ;
}
}
}