2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp

 秋实大哥の恋爱物语

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/contest/show/61

Description

传说有这么一个故事!

在一个月白风清的晚上,秋实大哥约一位他心仪的妹子一起逛校园,浪漫的秋实大哥决定在当晚对妹子表白。“XXXXX…”,秋实大哥温情地说完了 准备已久的话。而妹子决定用一种浪漫的方式接受秋实大哥(其实妹子早已对秋实大哥动心,这一刻她早已迫不及待了,但还是决定考秋实大哥最后一关,再委婉地 接受)。妹子拿出了她心爱的口琴,吹出了一首迷人的曲子…… “你能把我的曲子重复一遍么?”,但考虑到万一秋实大哥没有做到而失去了赢得人赢的心的机会,妹子又说到,“只要你能吹出我的一部分旋律,我就答应你,从 今以后,我就是你的一部分”。

好奇心重的你,真的很想知道秋实大哥最终有没有抱得美人归,除此之外,你还想知道秋实大哥吹出的曲子的旋律有多少次符合妹子的旋律。

《2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp》

将两个相邻的音符连起来,则妹子吹出的音符可以画出一条折线A,同样,秋实大哥吹出的音符也可以画出一条折线B,如果折线B已经与折线A的某一段完全重合,或者能够经过上下左右平移与折线A的某一段完全重合,则表示秋实大哥吹出了妹子的一部分旋律。

Input

第一行输入一个整数N(2≤N≤2⋅106),表示妹子吹了N个音符。

第二行输入N个音符,每个音符都是整数,且在32位整数范围内,每两个音符用一个空格隔开。

第三行输入一个整数M(2≤M≤2⋅106),表示秋实大哥吹了M个音符。

最后一行输入M个音符,每个音符都是整数,且在32位整数范围内,每两个音符用一个空格隔开。

Output

如果秋实大哥抱得美人归了,第一行输出Wow! Life Winner!,第二行再输出一个整数,表示秋实大哥的曲子的旋律有多少次符合妹子的。

如果秋实大哥没有做到,输出Oh. That's impossible. I must have had a dream.

 

Sample Input

26
1 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 1 0 1 0
5
1 1 0 1 1

Sample Output

Oh. That’s impossible. I must have had a dream.

HINT

 

题意

 

题解:

kmp基础题

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000010
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
//**************************************************************************************
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}


int a[maxn];
int b[maxn];
int c[maxn];
int d[maxn];
int p[maxn];
int main()
{
    int n=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    int m=read();
    for(int i=1;i<=m;i++)
        b[i]=read();
    n--;
    for(int i=1;i<=n;i++)
        c[i]=a[i+1]-a[i];
    m--;
    for(int i=1;i<=m;i++)
        d[i]=b[i+1]-b[i];
    int j=0;
    for(int i=2;i<=m;i++)
    {
        while(j>0&&d[j+1]!=d[i])j=p[j];
        if(d[j+1]==d[i])j++;
        p[i]=j;
    }
    j=0;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        while(j>0&&d[j+1]!=c[i])j=p[j];
        if(d[j+1]==c[i])j++;
        if(j==m){ans++;j=p[j];}
    }
    if(ans)
        printf("Wow! Life Winner!\n%d\n",ans);
    else
        printf("Oh. That's impossible. I must have had a dream.\n");
}

 

    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/4489808.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞