Language:
Oulipo
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:
Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces. So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a textT, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap. Input The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
Output For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T. Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN |
求模式串在主串中出现的次数
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1000010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
char s[10010],t[MAXN];
int slen,tlen;
int next[10010];//s为模式串 t为主串
void getNext()
{
int j=0,k=-1;
next[0]=-1;
while(j<slen)
{
if(k==-1||s[j]==s[k])
next[++j]=++k;
else
k=next[k];
}
}
int KMP_Count()
{
int ans=0;
int i,j=0;
if(slen==1&&tlen==1)
{
if(s[0]==t[0]) return 1;
else return 0;
}
getNext();
for(i=0;i<tlen;i++)
{
while(j>0&&t[i]!=s[j])
j=next[j];
if(s[j]==t[i]) j++;
if(j==slen)
{
ans++;
j=next[j];
}
}
return ans;
}
int main()
{
// fread;
int tc;
scanf("%d",&tc);
while(tc--)
{
scanf("%s%s",s,t);
slen=strlen(s);
tlen=strlen(t);
int ans=KMP_Count();
printf("%d\n",ans);
}
return 0;
}
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15948 Accepted Submission(s): 7030
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
6 -1
求模式串在主串中第一次出现的位置(从1开始算)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
int n,m;
int a[1000010],b[10010];
int NEXT[10010];
void getNEXT()
{
int j=0,k=-1;
NEXT[0]=-1;
while(j<m)
{
if(k==-1||b[j]==b[k])
NEXT[++j]=++k;
else
k=NEXT[k];
}
}
//返回首次出现的位置
int KMP_Index()
{
int i=0,j=0;
getNEXT();
while(i<n&&j<m)
{
if(j==-1||a[i]==b[j])
{
i++;
j++;
}
else
j=NEXT[j];
}
if(j==m) return i-m+1;
else return -1;
}
int main()
{
// fread;
int tc;
scanf("%d",&tc);
while(tc--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
int ans=KMP_Index();
printf("%d\n",ans);
}
return 0;
}
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 38422 | Accepted: 15947 |
Description
Input
Output
Sample Input
abcd aaaa ababab .
Sample Output
1 4 3
给定一个字符串 求这个字符串最多由几个相同的字符串组成
next表示模式串如果第i位(设str[0]为第0位)与文本串第j位不匹配则要回到第next[i]位继续与文本串第j位匹配。则模式串第1位到next[n]与模式串第n-next[n]位到n位是匹配的。如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
char ch[1000010];
int next[1000010];
int len;
void getNext()
{
int j=0,k=-1;
next[0]=-1;
while(j<len)
{
if(k==-1||ch[j]==ch[k])
next[++j]=++k;
else
k=next[k];
}
}
int main()
{
// fread;
while(scanf("%s",ch)!=EOF)
{
if(strcmp(ch,".")==0) break;
len=strlen(ch);
getNext();
if(len%(len-next[len])==0&&len/(len-next[len])>1)
printf("%d\n",len/(len-next[len]));
else
puts("1");
}
return 0;
}