题目传送:HDU - 1711 - Number Sequence
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
int n, m;
int a[1000005];
int b[10005];
int main() {
int T;
scanf("%d", &T);
while(T --) {
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i ++) {
scanf("%d", &a[i]);
}
for(int j = 0; j < m; j ++) {
scanf("%d", &b[j]);
}
int next[10005] = {-1};
int i = 0, j = -1;
while(i < m) {
if(j == -1 || b[i] == b[j]) {
next[++ i] = ++ j;
}
else {
j = next[j];
}
}
int ans, flag = 0;
i = 0, j = 0;
while(i < n) {
if(j == -1 || a[i] == b[j]) {
++ i; ++ j;
}
else {
j = next[j];
}
if(j == m) {
flag = 1;
ans = i - m + 1;
break;
}
}
if(flag == 1) {
printf("%d\n", ans);
}
else {
printf("-1\n");
}
}
return 0;
}
题目传送:HDU - 1686 - Oulipo
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
char W[10005];
char str[1000005];
int main() {
int T;
scanf("%d", &T);
while(T --) {
scanf("%s %s", W, str);
int next[10005] = {-1};
int i = 0, j = -1, len = strlen(W);
while(i < len) {
if(j == -1 || W[i] == W[j]) next[++ i] = ++ j;
else j = next[j];
}
i = 0, j = 0;
int ans = 0;
int len1 = strlen(str);
while(i < len1) {
if(j == -1 || W[j] == str[i]) i ++, j ++;
else j = next[j];
if(j == len) ans ++;
}
printf("%d\n", ans);
}
return 0;
}
题目传送:HDU - 2087 - 剪花布条
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
char str[1005];
char mo[1005];
int main() {
while(scanf("%s", str) != EOF) {
if(strcmp(str, "#") == 0) break;
scanf("%s", mo);
int next[1005] = {-1};
int i = 0, j = -1, len = strlen(mo);
while(i < len) {
if(j == -1 || mo[i] == mo[j]) next[++ i] = ++ j;
else j = next[j];
}
i = 0, j = 0;
int ans = 0, len1 = strlen(str);
while(i < len1) {
if(j == -1 || str[i] == mo[j]) i ++, j ++;
else j = next[j];
if(j == len) {
ans ++;
j = 0;
}
}
printf("%d\n", ans);
}
return 0;
}
题目传送:HDU - 3746 - Cyclic Nacklace
关于最小循环节
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
char s[100005];
int main() {
int T;
scanf("%d", &T);
while(T --) {
scanf("%s", s);
int len = strlen(s);
int next[100005] = {-1};
int i = 0, j = -1;
while(i < len) {
if(j == -1 || s[i] == s[j]) next[++ i] = ++ j;
else j = next[j];
}
if(next[len] == 0) {
printf("%d\n", len);
continue;
}
int t = len - next[len];//t为最小循环节
if(len % t == 0) {
printf("0\n");
}
else printf("%d\n", t - len % t); //len%t为除去所有最小循环节后多出来的
}
return 0;
}