您的位置:首页 > 产品设计 > UI/UE

[HDOJ1711]Number Sequence

2015-09-03 19:34 459 查看
题目链接: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): 15683 Accepted Submission(s): 6898


[align=left]Problem Description[/align]
Given two sequences of numbers : a[1], a[2], ...... , a
, 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.

[align=left]Input[/align]
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
. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].

[align=left]Output[/align]
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

6
-1

初学KMP,水一发模版题

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>

using namespace std;
const int maxn = 1000010;
int na, nb;
int a[maxn];
int b[maxn];
int pre[maxn];

void getpre(int *b, int *pre) {
int j, k;
pre[0] = -1;
j = 0;
k = -1;
while(j < nb - 1) {
if(k == -1 || b[j] == b[k]) {//匹配
j++;
k++;
pre[j] = k;
}
else {  //b[j] != b[k]
k = pre[k];
}
}
}

int kmp() {
int i = 0;
int j = 0;
getpre(b, pre);
while(i < na) {
if(j == -1 || a[i] == b[j]) {
i++;
j++;
}
else {
j = pre[j];
}
if(j == nb) {
return i - nb + 1;
}
}
return -1;
}

int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &na, &nb);
for(int i = 0; i < na; i++) {
scanf("%d", &a[i]);
}
for(int i = 0; i < nb; i++) {
scanf("%d", &b[i]);
}
printf("%d\n", kmp());
}
return 0;
}


本题还可以用hash做,效率与kmp差距不大。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <vector>

using namespace std;

typedef unsigned long long ull;
const int B = 100007;
const int maxn = 1000010;
int a[maxn], b[maxn];
int na, nb;

ull quickmul(int x, int n) {
ull ans = 1;
ull t = x;
while(n) {
if(n & 1) {
ans = (ans * t);
}
t = t * t;
n >>= 1;
}
return ans;
}

int contain() {
if(na > nb) {
return false;
}
ull t = quickmul(B, na);
ull ah = 0, bh = 0;
for(int i = 0; i < na; i++) {
ah = ah * B + a[i];
}
for(int i = 0; i < na; i++) {
bh = bh * B + b[i];
}
for(int i = 0; i + na <= nb; i++) {
if(ah == bh) {
return i + 1;
}
if(i + na < nb) {
bh = bh * B + b[i+na] - b[i] * t;
}
}
return -1;
}
int main() {
// freopen("in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &nb, &na);
for(int i = 0; i < nb; i++) {
scanf("%d", &b[i]);
}
for(int i = 0; i < na; i++) {
scanf("%d", &a[i]);
}
printf("%d\n", contain());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: