K - 秋实大哥の恋爱物语

Time Limit: 5000/2000MS (Java/Others)     Memory Limit: 32000/32000KB (Java/Others)
Submit Status

传说有这么一个故事!

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

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

将两个相邻的音符连起来,则妹子吹出的音符可以画出一条折线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 and output

Sample Input Sample Output
14
1 1 5 5 6 6 5 4 4 3 3 2 2 1
14
0 0 4 4 5 5 4 3 3 2 2 1 1 0
Wow! Life Winner!
1
20
1 2 1 2 1 2 1 2 1 1 0 1 3 2 3 2 7 6 7 2
3
6 5 6
Wow! Life Winner!
6
25
2 3 2 3 3 2 3 3 3 2 3 2 2 3 3 2 2 2 3 3 3 3 2 3 3
3
2 3 3
Wow! Life Winner!
5
29
6 6 7 5 5 6 4 4 5 3 3 4 2 2 3 1 1 2 0 0 1 -1 -1 0 -2 -2 -1 -3 -3
8
6 6 7 5 5 6 4 4
Wow! Life Winner!
8
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
Oh. That's impossible. I must have had a dream.

解题报告:

本题的正解是作差分再跑kmp..因为自己当时比较没想多。。构造的是最长前缀后缀等差数列....所以在失陪的时候还需要更改dis...

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 2e6+;
int front[maxn],s1[maxn],s2[maxn],len1,len2; void build_front()
{
front[] = -;
int t1 = - ,t2 = ,dis;
while(t2 < len2)
{
if (t1 == - || s2[t2] - s2[t1] == dis)
{
if (t1 == -)
{
dis = s2[t2] - s2[++t1];
if (t2 == )
t1--;
}
front[++t2] = ++t1;
}
else
t1 = front[t1];
}
} int kmp_search()
{
int t1 = , t2 = , ans = , dis = s1[] - s2[];
while(t1 < len1)
{
if (t2 == - || s1[t1] - s2[t2] == dis )
{
if (t2 == -)
dis = s1[t1] - s2[++t2];
t1++,t2++;
}
else
{
t2 = front[t2];
dis = s1[t1-] - s2[t2-];
}
if (t2 == len2)
{
ans ++;
t2 = front[t2];
dis = s1[t1-] - s2[t2-];
}
}
return ans;
} int main(int argc,char *argv[])
{
scanf("%d",&len1);
for(int i = ; i < len1 ; ++ i)
scanf("%d",&s1[i]);
scanf("%d",&len2);
for(int i = ; i < len2 ; ++ i)
scanf("%d",&s2[i]);
build_front();
int ans = kmp_search();
if (ans)
printf("Wow! Life Winner!\n%d\n",ans);
else
printf("Oh. That's impossible. I must have had a dream.\n");
return ;
}

UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>的更多相关文章

  1. UESTC_秋实大哥与时空漫游 2015 UESTC Training for Graph Theory<Problem C>

    C - 秋实大哥与时空漫游 Time Limit: 4500/1500MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Su ...

  2. UESTC_秋实大哥与连锁快餐店 2015 UESTC Training for Graph Theory<Problem A>

    A - 秋实大哥与连锁快餐店 Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) S ...

  3. UESTC_全都是秋实大哥 2015 UESTC Training for Search Algorithm & String<Problem J>

    J - 全都是秋实大哥 Time Limit: 5000/2000MS (Java/Others)     Memory Limit: 32000/32000KB (Java/Others) Subm ...

  4. UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>

    M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  5. UESTC_秋实大哥与妹纸 2015 UESTC Training for Data Structures<Problem F>

    F - 秋实大哥与妹纸 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 1500/1500KB (Java/Others) Submit ...

  6. UESTC_Palindromic String 2015 UESTC Training for Search Algorithm & String<Problem M>

    M - Palindromic String Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 128000/128000KB (Java ...

  7. UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>

    N - 韩爷的梦 Time Limit: 200/100MS (Java/Others)     Memory Limit: 1300/1300KB (Java/Others) Submit Stat ...

  8. UESTC_吴队长征婚 2015 UESTC Training for Search Algorithm & String<Problem E>

    E - 吴队长征婚 Time Limit: 10000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  9. UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>

    D - 基爷的中位数 Time Limit: 5000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

随机推荐

  1. statfs函数说明

    函数:     statfs 功能描述:     查询文件系统相关的信息. 用法:     #include <sys/vfs.h> /* 或者 <sys/statfs.h> ...

  2. Subsets II 解答

    Question Given a collection of integers that might contain duplicates, nums, return all possible sub ...

  3. hdu4453-Looploop(伸展树)

    题目有很多图,不好粘贴..... 题意:给出N个数和K1,K2的值,最开始指针指向第一个数,有6种操作 add x : 给前K2个数都增加x reverse : 翻转前K1个数 insert x : ...

  4. jdbc资料收集

    1.Hibernate史上最简单的Hibernate入门简介http://blog.csdn.net/doodoofish/article/details/43207/ jdbc不足 尽管JDBC在J ...

  5. awk笔记1

    grep: 文本过滤器    grep 'pattern' input_file ... sed:流编辑器 awk: 报告生成器    格式化以后,显示 AWK a.k.a. Aho, Kernigh ...

  6. ps查看内存占用排序

    ps -eo rss,pmem,pcpu,vsize,args | sort -k 1 -r -n | less 解析一下: ps 都知道,是linux,unix显示进程信息的, -e 是显示所有进程 ...

  7. VPN拨号后使用本地网络上网

    网络环境大概是这样了:我在家里用ADSL上网,通过VPN连接到公司的服务器.但是连接VPN后,只能登录到公司的服务器,与INTERNET就断开了,QQ.网页都断开了.公司的服务器应该是连网的,可能被限 ...

  8. C语言漫谈(二) 图像显示 Windows和Linux

    关于图像显示有很多库可以用,Windows下有GDI,GDI+,D3D等,Linux下有X Window和Wayland,此外还有OpenGL ,SDL等图形库以及各种GUI库. 了解最原始的方式,对 ...

  9. 某APK中使用了动态注册BroadcastReceiver,Launcher中动态加载此APK出现java.lang.SecurityException异常的解决方法

    在某APK中,通过如下方法动态注册了一个BroadcastReceiver,代码参考如下: @Override protected void onAttachedToWindow() { super. ...

  10. [Hapi.js] Request Validation with Joi

    hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...