题目:Oulipo
连接:https://vjudge.net/contest/278481#problem/A
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
char a[10005], b[1000005];
int next[1000005];
void GetNext(char a[])
{
next[0] = -1;
int j = -1, i = 0;
while(i < strlen(a))
{
if(j == -1 || a[i] == a[j])
{
next[++i] = ++j;
}
else
{
j = next[j];
}
}
}
int sum;
void KMP(char a[], char b[])
{
int n = strlen(a), m = strlen(b);
int i = 0, j = 0;
GetNext(b);
while(i < n)
{
if(j == -1 || a[i] == b[j]){
i++;
j++;
}
else j = next[j];
if(j >= m)
{
sum++;
i--;
j--;
j = next[j];
}
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", a);
scanf("%s", b);
sum = 0;
KMP(b, a);
printf("%d\n", sum);
}
return 0;
}
题目:Number Sequence
链接:https://vjudge.net/contest/278481#problem/B
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int a[1000005], b[10005];
int ne[10005];
void Getne(int a[], int n)
{
ne[0] = -1;
int j = -1, i = 0;
while(i < n)
{
if(j == -1 || a[i] == a[j])
{
ne[++i] = ++j;
}
else
{
j = ne[j];
}
}
}
int sum;
int KMP(int a[], int n, int b[], int m)
{
int i = 0, j = 0;
Getne(b, m);
int flag = 0;
while(i < n)
{
if(j == -1 || a[i] == b[j]){
i++;
j++;
}
else j = ne[j];
if(j == m)
{
flag = 1;
break;
}
}
if(flag)return i - j+1;
else return -1;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n, m;
cin>>n>>m;
for(int i = 0; i < n; i++)cin>>a[i];
for(int j = 0; j < m; j++)cin>>b[j];
printf("%d\n", KMP(a, n, b, m));
}
return 0;
}
题目:Power Strings
链接:https://vjudge.net/contest/278481#problem/D
代码:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int next[1000005];
int t;
void getNext(char a[]){
int i = 0, j = -1;
next[0] = -1;
while(i < t){
if(j == -1 || a[i] == a[j]){
i++;
j++;
next[i] = j;
}
else {
j = next[j];
}
}
}
int main(){
char a[1000005];
while(scanf("%s", a) && a[0] != '.'){
t= strlen(a);
getNext(a);
int k = t - next[t];
if(t % k == 0)
cout<<t / k<<endl;
else cout<<1<<endl;
}
return 0;
}
题目:Count the string
链接:https://vjudge.net/contest/278481#problem/E
代码:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int ne[200005], vis[200005];
int n;
void getne(char a[]){
int i = 0, j = -1;
ne[0] = -1;
while(i < n){
if(j == -1 || a[i] == a[j]){
i++;
j++;
ne[i] = j;
}
else {
j = ne[j];
}
}
}
int main(){
char a[200005];
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
scanf("%s", a);
int ans = n;//每一个字母自己本身就是一次
getne(a);
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++){
vis[ne[i]]++;
}
for(int i = 1; i <= n; i++){
ans += vis[i];
ans %= 10007;
}
cout<<ans<<endl;
}
return 0;
}
题目:Simpsons’ Hidden Talents
链接:https://vjudge.net/contest/278481#problem/G
代码:
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
int ne[100005];
char a[100005], b[100005];
int main(){
while(~scanf("%s %s", a, b)){
int len1 = strlen(a), len2 = strlen(b);
t = len1 + len2;
strcat(a, b);
getne(a);
int k = ne[t];
while(k > len1 || k > len2){//防止aaa aaaa这种情况 abab abababab 这种重复的情况
k = ne[k];
}
if(k == 0)printf("0\n");
else {
for(int i = 0; i < k ; i++){
printf("%c", a[i]);
}
printf(" %d\n", k);
}
}
return 0;
}
题目:周期
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000100;
char str[maxn];
int Next[maxn];
int n;
void get_next()
{
for(int i = 2, j = 0; i <= n; i++)
{
while(j && str[i] != str[j+1])j = Next[j];
if(str[i] == str[j+1])j++;
Next[i] = j;
}
}
int main()
{
int ca= 0;
while(scanf("%d", &n), n)
{
scanf("%s", str+1);
get_next();
printf("Test case #%d\n", ++ca);
for(int i = 2; i <= n; i++)
{
int t = (i- Next[i]);
//cout<<Next[i]<<endl;
if(i % t == 0 && i > t)cout<<i<<" "<<i/t<<endl;
}
cout<<endl;
}
return 0;
}