题目
代码
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
/**
* Created by Enzo Cotter on 2019/4/30.
*/
public class Main {
public static int [] next = new int[10003];
public static int n, m;
public static int [] a = new int[1000004];
public static int [] b = new int[10004];
public static void main(String[] args) {
Scanner input = new Scanner(new BufferedInputStream(System.in));
PrintWriter out = new PrintWriter(System.out);
int T = input.nextInt();
while (T-- > 0) {
n = input.nextInt();
m = input.nextInt();
for (int i = 1; i <= n; i++)
a[i] = input.nextInt();
for (int i = 1; i <= m; i++)
b[i] = input.nextInt();
Arrays.fill(next, 0 );
for (int i = 2, j = 0; i <= m; i++) {
while (j > 0 && b[i] != b[j + 1]) j = next[j];
if (b[i] == b[j + 1])
j++;
next[i] = j;
}
int flag = -1;
for (int i = 1, j = 0; i <= n; i++) {
while (j > 0 && (j == m || a[i] != b[j + 1])) j = next[j];
if (a[i] == b[j + 1])j++;
if (j == m) {
flag = i - j + 1;
break;
}
}
out.println(flag);
}
out.flush();
}
}
题目
代码
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
/**
* Created by Enzo Cotter on 2019/4/30.
*/
public class Main {
public static int [] next = new int[10003];
public static int n, m;
public static void main(String[] args) {
Scanner input = new Scanner(new BufferedInputStream(System.in));
PrintWriter out = new PrintWriter(System.out);
while (input.hasNext()) {
String a = input.next();
if (a.charAt(0)=='#')break;
String b = input.next();
n = a.length();
m = b.length();
Arrays.fill(next, 0 );
for (int i = 2, j = 0; i <= m; i++) {
while (j > 0 && b.charAt(i - 1) != b.charAt(j)) j = next[j];
if (b.charAt(i - 1) == b.charAt(j))
j++;
next[i] = j;
}
int cnt = 0;
for (int i = 1, j = 0; i <= n; i++) {
while (j > 0 && (j == m || a.charAt(i - 1) != b.charAt(j))) j = next[j];
if (a.charAt(i - 1) == b.charAt(j))j++;
if (j == m) {
cnt++;
j = 0;
}
}
out.println(cnt);
}
out.flush();
}
}
题目
代码
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
/**
* Created by Enzo Cotter on 2019/4/30.
*/
public class Main {
public static int [] next = new int[200003];
public static int n, m;
public static void main(String[] args) {
Scanner input = new Scanner(new BufferedInputStream(System.in));
PrintWriter out = new PrintWriter(System.out);
int [] dp = new int[200004];
int t = input.nextInt();
while (t-- > 0) {
int n = input.nextInt();
String s = input.next();
Arrays.fill(next, 0);
Arrays.fill(dp,1);
for (int i = 2, j = 0; i <= n; i++) {
while (j > 0 && s.charAt(i - 1) != s.charAt(j)) j = next[j];
if (s.charAt(i - 1) == s.charAt(j))j++;
next[i] = j;
}
for (int i = 1; i <= n; i++) {
dp[next[i]]++;
}
long sum = 0;
for (int i = 1; i <= n; i++)
sum = (sum + dp[i])%10007;
out.println(sum);
}
out.flush();
}
}
题目
题意
求最少需要添加字符的个数,使得整个串可以表示为某个串循环得到的;
代码
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
const int N = 1000002;
int nxt[N];
char T[N];
int tlen;
void getNext()
{
int j, k;
j = 0; k = -1;
nxt[0] = -1;
while(j < tlen)
if(k == -1 || T[j] == T[k])
{
nxt[++j] = ++k;
if (T[j] != T[k]) //се╩╞
nxt[j] = k;
}
else
k = nxt[k];
}
int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%s",&T);
tlen= strlen(T);
getNext();
if(nxt[tlen]==0)
{
printf("%d\n",tlen) ;
continue;
}
if(tlen%(tlen-nxt[tlen])==0)
printf("0\n");
else
{
printf("%d\n",tlen-nxt[tlen]-tlen%(tlen-nxt[tlen]));
}
}
return 0;
}
题目
题意
求子串与主串匹配的次数,可以重叠
代码
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
/**
* Created by Enzo Cotter on 2019/4/30.
*/
public class Main {
public static int [] next = new int[10003];
public static int n, m;
public static void main(String[] args) {
Scanner input = new Scanner(new BufferedInputStream(System.in));
PrintWriter out = new PrintWriter(System.out);
int T = input.nextInt();
while (T-- > 0) {
String b = input.next(), a = input.next();
n = a.length();
m = b.length();
Arrays.fill(next, 0 );
for (int i = 2, j = 0; i <= m; i++) {
while (j > 0 && b.charAt(i - 1) != b.charAt(j)) j = next[j];
if (b.charAt(i - 1) == b.charAt(j))
j++;
next[i] = j;
}
int cnt = 0;
for (int i = 1, j = 0; i <= n; i++) {
while (j > 0 && (j == m || a.charAt(i - 1) != b.charAt(j))) j = next[j];
if (a.charAt(i - 1) == b.charAt(j))j++;
if (j == m) {
cnt++;
j = next[j];
}
}
out.println(cnt);
}
out.flush();
}
}
题目
题意
对于一个字符串的所有前缀,如果整个前缀可以表示为某个子串重复 k k k次,就输出整个串的长度及这个k;
代码
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
/**
* Created by Enzo Cotter on 2019/4/30.
*/
public class Main {
public static int [] next = new int[1000003];
public static int n, m;
public static void main(String[] args) {
Scanner input = new Scanner(new BufferedInputStream(System.in));
PrintWriter out = new PrintWriter(System.out);
int cnt = 0;
while (input.hasNext()) {
int n = input.nextInt();
if (n == 0)break;
out.println("Test case #" + ++cnt);
String s = input.next();
Arrays.fill(next, 0);
for (int i = 2, j = 0; i <= n; i++) {
while (j > 0 && s.charAt(i - 1) != s.charAt(j)) j = next[j];
if (s.charAt(i - 1) == s.charAt(j))j++;
next[i] = j;
}
for (int i = 2; i <= n; i++) {
if (next[i] == 0)continue;
if (i % (i - next[i]) == 0) {
out.println(i + " " + i / (i - next[i]));
}
}
out.println();
}
out.flush();
}
}
题目
题意
对于给定的字符串 s s s, s s s可以表示为 a n a^n an,输出这个最大的 n n n;
代码
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
public class Main {
public static int [] next = new int[1000003];
public static int n, m;
public static void main(String[] args) {
Scanner input = new Scanner(new BufferedInputStream(System.in));
PrintWriter out = new PrintWriter(System.out);
while (input.hasNext()) {
String s = input.next();
if (s.equals("."))break;
n = s.length();
Arrays.fill(next, 0);
for (int i = 2, j = 0; i <= n; i++) {
while (j > 0 && s.charAt(i - 1) != s.charAt(j)) j = next[j];
if (s.charAt(i - 1) == s.charAt(j))j++;
next[i] = j;
}
if (n % (n - next[n]) ==0)
out.println(n / (n - next[n]));
else out.println(1);
}
out.flush();
}
}