Beta Round #9 (酱油杯noi考后欢乐赛)PLQ和他的小伙伴们
题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFnoi%E8%80%83%E5%90%8E%E6%AC%A2%E4%B9%90%E8%B5%9B%29/PLQ%E5%92%8C%E4%BB%96%E7%9A%84%E5%B0%8F%E4%BC%99%E4%BC%B4%E4%BB%AC
题解:先给题目点个赞!真是一道好题!
刚开始看见真是一点思路都没有,后来看见简要题解说KMP,不知道怎么用。。。
今天忽然有了思路,我们假设从1开始,然后可以得到一个惊呆序列,并且把每个人的惊呆指数记下来,得到一个数列a[i]。
那么这个数列是由从谁开始数1唯一确定的,比如从i开始i最后的惊讶指数就是a[1],i+1就是a[2],也就是我们保持了一个相对的位置关系。
那我们对由a对b做字符串匹配,(先把a在后面复制一遍),假设匹配成功点是i,那么i-n+1就是1是从起始点开始数的第几个人,我们就可以得出起始点是谁。
一道漂亮的题!
推起始点那里蛋疼了好久。。。
代码:
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<set> #include<queue> #include<string> #define inf 1000000000 #define maxn 2000000+5 #define maxm 500+100 #define eps 1e-10 #define ll long long #define pa pair<int,int> #define for0(i,n) for(int i=0;i<=(n);i++) #define for1(i,n) for(int i=1;i<=(n);i++) #define for2(i,x,y) for(int i=(x);i<=(y);i++) #define for3(i,x,y) for(int i=(x);i>=(y);i--) #define mod 1000000007 using namespace std; inline int read() { int x=,f=;char ch=getchar(); while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();} while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();} return x*f; }
int n,m,a[maxn],b[maxn],next[maxn],ans[maxn],tot; int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();m=read();int x=-;
for1(i,m)a[((x+=read())%=n)+]++;
for1(i,n)a[n+i]=a[i];
for1(i,n)b[i]=read();
int j=;
for2(i,,n)
{
while(j&&b[j+]!=b[i])j=next[j];
if(b[j+]==b[i])j++;
next[i]=j;
}
j=;
for1(i,*n-)
{
while(j&&b[j+]!=a[i])j=next[j];
if(b[j+]==a[i])j++;
if(j==n)ans[++tot]=(*n+-i)%n?(*n+-i)%n:n,j=next[j];
}
if(tot!=){printf("Cannot determine!\n");printf("%d\n",tot);}else printf("%d\n",ans[]); return ; }
改了其中的一些细节,好像更容易理解?
代码:
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<set> #include<queue> #include<string> #define inf 1000000000 #define maxn 2000000+5 #define maxm 500+100 #define eps 1e-10 #define ll long long #define pa pair<int,int> #define for0(i,n) for(int i=0;i<=(n);i++) #define for1(i,n) for(int i=1;i<=(n);i++) #define for2(i,x,y) for(int i=(x);i<=(y);i++) #define for3(i,x,y) for(int i=(x);i>=(y);i--) #define mod 1000000007 using namespace std; inline int read() { int x=,f=;char ch=getchar(); while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();} while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();} return x*f; }
int n,m,a[maxn],b[maxn],next[maxn],ans,tot; int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();m=read();int x=-;
for1(i,m)a[((x+=read())%=n)+]++;
for1(i,n)a[n+i]=a[i];
for1(i,n)b[i]=read();
int j=;
for2(i,,n)
{
while(j&&b[j+]!=b[i])j=next[j];
if(b[j+]==b[i])j++;
next[i]=j;
}
j=;
for1(i,*n-)
{
while(j&&b[j+]!=a[i])j=next[j];
if(b[j+]==a[i])j++;
if(j==n)tot++,ans=-i+n+n,j=next[j];
}
if(tot!=){printf("Cannot determine!\n");printf("%d\n",tot);}else printf("%d\n",(ans-)%n+); return ; }
Beta Round #9 (酱油杯noi考后欢乐赛)PLQ和他的小伙伴们的更多相关文章
- Beta Round #9 (酱油杯noi考后欢乐赛)PLQ的寻宝
题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...
- Beta Round #9 (酱油杯noi考后欢乐赛)乌鸦喝水
题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...
- Beta Round #9 (酱油杯noi考后欢乐赛)随机数生成器
题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...
- Beta Round #9 (酱油杯noi考后欢乐赛)最大伤害
题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...
- 洛谷---小L和小K的NOIP考后放松赛
链接: https://www.luogu.org/contestnew/show/11805?tdsourcetag=s_pcqq_aiomsg 题解: 没人过的题我就没看 t2: 考虑每个点是朋友 ...
- Codeforces Beta Round #57 (Div. 2)
Codeforces Beta Round #57 (Div. 2) http://codeforces.com/contest/61 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #52 (Div. 2)
Codeforces Beta Round #52 (Div. 2) http://codeforces.com/contest/56 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #46 (Div. 2)
Codeforces Beta Round #46 (Div. 2) http://codeforces.com/contest/49 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #31 (Div. 2, Codeforces format)
Codeforces Beta Round #31 (Div. 2, Codeforces format) http://codeforces.com/contest/31 A #include< ...
随机推荐
- OC单例模式的实现
SingleClass.m #import <Foundation/Foundation.h> @class SingleClass; static SingleClass *instan ...
- 在js中window.open通过“post”传递参数
在js中window.open通过“post”传递参数的步骤如下: 如:在A.jsp中 有一个js方法 winow.open,目标地址是 xx.do 1.在A.jsp建一个form,把要设置的值通过j ...
- java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
一,File创建文件 File file = new File("D:" + File.separator + "yi.txt"); 代码示例: package ...
- UITextAlignmentCenter' is deprecated: first deprecated in iOS 6.0
- linux驱动(一)
编写模块必须先声明下面两句: #include <linux/module.h> //这个头文件包含了许多符号与函数的定义,这些符号与函数多与加载模块有关 #i ...
- input表单
submit:点击submit按钮表单就会被提交给服务器,中文IE下默认按钮文本为“提交查询”,可以设置value属性修改按钮的显示文本 text:size属性为宽度,value为值,maxlengt ...
- javascript 笔记(1)
document.forms[0].names.value == '' forms[0]表示文档的第一个 form 表单,names 表示这个表单元素名为 names 的表单的元素,此时表示的是获取此 ...
- 你需要了解的z-index世界
本文摘自:飘零雾雨的博客 z-index的重要性 在我看来,z-index 给了我们日常工作中以极大的帮助,我们用它来定义元素的层叠级别(stack level).受益于它,你能做Popup, Dro ...
- 【python】python的列表表达式或解析式,帅就一个字
>>> list1 = [x**2 for x in range(10)]>>> list1[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
- 基于jsp+servlet图书管理系统之后台用户信息查询操作
上一篇的博客写的是插入操作,且附有源码和数据库,这篇博客写的是查询操作,附有从头至尾写的代码(详细的注释)和数据库! 此次查询操作的源码和数据库:http://download.csdn.net/de ...