機試之字符串匹配——HDU1711-Number Sequence
1、KMP原理及代碼
1.1KMP原理
KMP是字符串匹配的一種較好的算法。字符串匹配在字符串的應用中很常見,其常見的方法有:
直接循環逐個比較,不符合則子串指針歸零,母串指針下移一位,可參考如下例子
這種方法耗時太大,爲了進一步改進,大佬們開發了KMP算法。KMP算法是對直接逐個比較的改進,所以其也是進行循環比較,不同之處在於匹配不符後,子串指針仍歸爲0,但是母串指針要找到一個在失配後面最符合的一個位置處,於是引入nextTable[]數組,專門來存儲該信息。
1.2KMP代碼
//機試之字符串匹配
//理論部分沒聽懂
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXM = 100;
int nextTable[MAXM];
void GetNextTable(string pattern){//構造next數組
int m = pattern.size();//獲取模式串大小
int j = 0;
nextTable[j] = -1;
int t = nextTable[j];
while(j < m){
if(t == -1 || pattern[t] == pattern[j]){
++t;
++j;
nextTable[j] = t;
}
else{
t = nextTable[t];
}
}
}
int KMP(string text,string pattern){//文本串和模式串,返回第一次匹配成功的文本串上的下標
GetNextTable(pattern);//構造next數組
int n = text.size();
int m = pattern.size();
int i = 0, j = 0;
while(i < n && j < m){
if(j == -1 || text[i] == pattern[j]){
++i;
++j;
}
else{
j = nextTable[j];
}
}
if(j == m){
return i - j;
}
else{
return -1;
}
}
int main(){
string text,pattern;
text = "I love you";
pattern = "love";
int pos = KMP(text,pattern);
cout<<pos<<endl;
return 0;
}
2、例題HDU1711Number Sequence
2.1題目
題目鏈接:
http://acm.hdu.edu.cn/showproblem.php?pid=1711
**Number Sequence**
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 58011 Accepted Submission(s): 23152
Problem Description
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.
Input
The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].
Output
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
Sample Input
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1
Sample Output
6
-1
Source
HDU 2007-Spring Programming Contest
2.2代碼
//機試之字符串匹配——HDU1711Number Sequence
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1000000;
const int MAXM = 10000;
int nextTable[MAXM];
int text[MAXN];
int pattern[MAXM];
void GetNextTable(int m){//構造next數組
int j = 0;
nextTable[j] = -1;
int t = nextTable[j];
while(j < m){
if(t == -1 || pattern[t] == pattern[j]){
++t;
++j;
nextTable[j] = t;
}
else{
t = nextTable[t];
}
}
}
int KMP(int n, int m){//文本串和模式串,返回第一次匹配成功的文本串上的下標
GetNextTable(m);//構造next數組
int i = 0, j = 0;
while(i < n && j < m){
if(j == -1 || text[i] == pattern[j]){
++i;
++j;
}
else{
j = nextTable[j];
}
}
if(j == m){
return i - j;
}
else{
return -1;
}
}
int main(){
int T;
// cin>>T;
scanf("%d",&T);
while(T--){//樣例數
int n,m;//文本串包含數個數與模式串包含數個數
// cin>>n>>m;
scanf("%d%d",&n,&m);
for(int i = 0; i < n; ++i){//輸入文本串
// cin>>text[i];
scanf("%d",&text[i]);
}
for(int i = 0; i < n; ++i){//輸入模式串
// cin>>pattern[i];
scanf("%d",&pattern[i]);
}
int res = KMP(n,m);
if(res == -1){
// cout<<-1<<endl;
printf("-1\n");
}
else{
// cout<<KMP(n,m)+1<<endl;
printf("%d\n",KMP(n,m)+1);
}
}
return 0;
}