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:
Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…
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 text T, 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:
One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.
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
Sample Output
1
3
0
前言:看到这道题,很惊喜。这不是kmp算法的简单变形吗?kmp算法是用来求小字符串s是否在大字符串S里。但是咋计数呢,好像没教QAQ,自己怕出错也不敢写。于是上网搜,搜半个小时,也找不到一个写的好的题解。算了我自己尝试着写吧,结果发现就加了个小变量就OK了QAQ,唉,还得靠自己,不能懒,多尝试。
题解:
算法:kmp算法
学过kmp算法,这道题很简单了,就不写解题思路了,主要写写我在这道题里遇到的bug—cin、cout用了会超时。
怎么办呢?
有一个神奇的东西叫关闭同步流,只有一行代码。
std::ios::sync_with_stdio(false);
以下关于同步流的描述摘自此博客https://blog.csdn.net/I_O_fly/article/details/89449575
很多C++的初学者可能会被这个问题困扰,经常出现程序无故超时,最终发现问题处在cin和cout上,(甚至有些老oier也会被这个问题困扰,每次只能打scanf和printf,然后一堆的占位符巨麻烦),这是因为C++中,cin和cout要与stdio同步,中间会有一个缓冲,所以导致cin,cout语句输入输出缓慢,这时就可以用这个语句,取消cin,cout与stdio的同步,说白了就是提速,效率基本与scanf和printf一致。然后就可放心的使用cin,cout了。
今天遇到有人问问题说关闭流同步以后会炸空间,我也很诧异。然后看了下代码,问题出在scanf()。取消流同步以后,stdio中带有的scanf()和printf()输入输出的内部同步也会被取消,这时候再用scanf()和printf()就可能会出玄学错误,所以用的时候也要注意。(意思就是如果你关闭了同步流,那么你就别用scanf和printf了)
另外,如果使用文件输入输出的话,一定记住要把这条语句放在freopen()后面,不然会报错。
AC代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
vector<int> cal_nxt(string s)
{
int n=(int)s.length();
vector<int> nxt(n);
nxt[0]=0;
for(int i=1;i<n;i++)
{
int j=nxt[i-1];
while(s[i]!=s[j]&&j>0) j=nxt[j-1];
if(s[i]==s[j]) j++;
nxt[i]=j;
}
return nxt;
}
int main(){
std::ios::sync_with_stdio(false);//关闭同步流
int T;
cin>>T;
while(T--)
{
string s,t;
cin>>t>>s;
int m=(int)t.length();
string T=t+'#'+s;
vector<int> nxt=cal_nxt(T);
int ans=0;
for(int i=m+1;i<(int)T.length();i++)
{
if(nxt[i]==m)
{
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
孤雁飞去 红颜来相许
待到酒清醒 她无影 原来是梦里 —许嵩《江湖》