首先说一下这是一道kmp的题目,今天刚跟崔老师学了kmp,这个算法是很基础的算法,崔老师竟然很惊奇的发现我不会啊、、呜呜。。sad啊、、还得多学习啊,在ACM的路上还差很多啊!
首先推荐一个博客http://www.cppblog.com/oosky/archive/2006/07/06/9486.html绝对的大神啊,写的真的很好啊。很详细啊,赞一个啊!
我说一下我自己的理解:
比如有两个串:ababc 和 abababc。假设第一个为子串,第二个为母串。
第一个先预处理一下找到自己循环的节点,记录位置,这样回溯的时候就可以直接找到,开始的位置,不用再从头向后遍历了啊。比如说判断到子串c的时候与母串的a不符合,这时候就回溯到第一个b的后面,然后判断是否相符,这样就可以接着遍历,找到匹配的字符串。可以节省很多的时间啊。(刚学的,理解可能不全面、、、)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18365 | Accepted: 7365 |
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:
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
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
string str1, str2;
int main()
{
int t, i, j, len1, len2, num;
int next[10100];
cin >>t;
while(t--)
{
next[0] = -1;
num = 0;
j = -1;
cin >>str1>>str2;
len1 = str1.length();
len2 = str2.length();
for(i = 1; i <= len1-1; i++)
{
while(j >= 0 && str1[j+1]!=str1[i])
j = next[j];
if(str1[j+1] == str1[i]) j++;
next[i] = j;
}
j = -1;
for(i = 0; i <= len2-1; i++)
{
while(j >= 0 && str1[j+1]!=str2[i])
j = next[j];
if(str1[j+1] == str2[i]) j++;
if(j == len1-1)
{
num++;
j = next[j];
}
}
cout <<num<<endl;
}
return 0;
}