POJ 3461 字符串出现次数 && HDU1711 字符串第一次出现的位置 模板题

 
Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 48387   Accepted: 19261

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 <iostream>
#include <stdio.h>
#include <cstring>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
typedef long long ll;
const int maxn=1e6+10,inf=0x3f3f3f3f;
const ll mod=1e9+7;
char t[maxn],s[maxn];
int _next[maxn];
int tlen,slen;
void getnext()
{
    int j,k;
    j=0,k=-1,_next[0]=-1;
    while(j<tlen)
        if(k==-1||t[j]==t[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<slen;i++)
    {
        while(j>0&&s[i]!=t[j])
            j=_next[j];
        if(s[i]==t[j]) j++;
        if(j==tlen)
        {
            ans++;j=_next[j];
        }
    }
    return ans;
}
int main()
{
    int _;
    scanf("%d",&_);
    while(_--)
    {
        scanf("%s",t);
        scanf("%s",s);
        tlen=strlen(t);
        slen=strlen(s);
        cout<<KMP_count()<<endl;
    }
}
View Code

两个板子题

Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 38246    Accepted Submission(s): 15812


Problem Description
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.
 

 

Input
The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].
 

 

Output
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
 

 

Sample Input
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
 
Sample Output
6
-1

 

 1 #include <bits/stdc++.h>
 2 #define pb push_back
 3 #define mp make_pair
 4 #define fi first
 5 #define se second
 6 #define all(a) (a).begin(), (a).end()
 7 #define fillchar(a, x) memset(a, x, sizeof(a))
 8 #define huan printf("\n");
 9 #define debug(a,b) cout<<a<<" "<<b<<" ";
10 using namespace std;
11 typedef long long ll;
12 const int maxn=1e6+10,inf=0x3f3f3f3f;
13 const ll mod=1e9+7;
14 int t[maxn],s[maxn];
15 int _next[maxn];
16 int tlen,slen;
17 void getnext()
18 {
19     int j,k;
20     j=0,k=-1,_next[0]=-1;
21     while(j<tlen)
22         if(k==-1||t[j]==t[k])
23             _next[++j]=++k;
24         else
25             k=_next[k];
26 }
27 int KMP_index()
28 {
29     int i=0,j=0;
30     getnext();
31     while(i<slen&&j<tlen)
32     {
33         if(j==-1||s[i]==t[j])
34             i++,j++;
35         else
36             j=_next[j];
37     }
38     if(j==tlen)
39         return i-tlen+1;
40     else
41         return -1;
42 }
43 int main()
44 {
45     int _;
46     scanf("%d",&_);
47     while(_--)
48     {
49         scanf("%d%d",&slen,&tlen);
50         for(int i=0;i<slen;i++)
51             scanf("%d",&s[i]);
52         for(int i=0;i<tlen;i++)
53             scanf("%d",&t[i]);
54         cout<<KMP_index()<<endl;
55     }
56 }
View Code

 

posted @ 2018-07-31 15:23  灬从此以后灬  阅读(251)  评论(0编辑  收藏  举报