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< ...
随机推荐
- jsp 页面获取xml的内容
<c:out value="${history.xml}" escapeXml="true" />
- java 简单分页/总结
模型(实体) dao层 dao的实现daoimpl层 service层 然后是servlet 把service层加载到servlet中就可以传值了,马上就能看见效果了 jsp页面来了 当然不能忘了在L ...
- 利用Highcharts制作web图表学习(一)
前言:最近项目中需要对数据进行汇总和分析得出柱状图或曲线图这样散装点图,经过学习和测试发现Highchart不错,如果大家项目中需要的话,不妨看看有的例子摘自官网 一.先说说HighCharts的 ...
- ios8地图开发的报错
报错如下:Trying to start MapKit location updates without prompting for location authorization. Must call ...
- STL unique使用问题
string strs[] = {"one","one","two","three","three" ...
- jira汉化,破解,升级
交给我这个任务,我先在网络上查了,好些资料,先实验的是6.3.6版本的,这个安装包我是从csdn上下载的.tar.gz的安装,汉化过程也都没有问题.但是在运行过程中不显示下拉菜单,于是我又在官网下载的 ...
- Trac与Apache的配合
将Trac与Apache配合使用,需要用到mod_wsgi模块,首先Apache要安装负责wsgi的模块. def application(environ, start_request): #... ...
- php tpl 模板页面如和给js文件传参数
有一个参数,服务器传给了php 模板页面,但模板包含的js需要得到这个参数值.如何处理: 一,在引入页面前加一句代码 <script type="text/javascript&quo ...
- 前端工程的构建工具对比 Gulp vs Grunt
1. Grunt -> Gulp 早些年提到构建工具,难免会让人联想到历史比较悠久的Make,Ant,以及后来为了更方便的构建结构类似的Java项目而出现的Maven.Node催生了一批自动化工 ...
- C# MySql 操作类
/* MySql 类 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; ...