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< ...
随机推荐
- C10K问题和Libevent库介绍
http://blog.chinaunix.net/uid-20761674-id-75056.html 一.C10K的问题 C10K的问题在上个世纪90年代就被提出来了.大概的意思是当用户数超过1万 ...
- c语言对于文本的基本操作
字符读写函数 :fgetc和fputc 字符串读写函数:fgets和fputs 数据块读写函数:freed和fwrite 格式化读写函数:fscanf和fprinf 1.字符读写: fgetc函 ...
- google模拟各种Android手机浏览器方法
在开始--运行 输入 chrome.exe --user-agent="Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/ ...
- 【培训】交换机VLAN
为了解决用交换机做LAN互联无法限制广播的问题,出现了VLAN技术,把一个LAN划分为多个逻辑的“LAN”-VLAN. VLAN技术将一个物理的LAN逻辑地划分为不同的广播域,每一个VLAN包含一组有 ...
- c++ primer复习(五):类
一:基本内容 1 类 数据成员:用于存储与类对象相关联的状态 成员函数:对数据成员进行操作 类将接口与实现分离,接口指定了类支持的操作,操作的具体实现细节是类的设计者才需要了解 2 类成员 类成员可以 ...
- SQL Server系统视图 [不定期更新]
1.sys.objects:在数据库中创建的每个用户定义的架构作用域内的对象(如表.视图.约束.默认值.日志.规则存储过程等,但不包括DDL触发器)在该表中均对应一行. 列名 说明 name 对象名. ...
- Invoke()方法的使用
在多线程编程中,我们经常要在工作线程中去更新界面显示,而在多线程中直接调用界面控件的方法是错误的做法,Invoke 和 BeginInvoke 就是为了解决这个问题而出现的,使你在多线程中安全的更新界 ...
- SQLServer:定时作业
SQLServer:定时作业: 如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理-SQL Server代理-作业来实现 也快可以定时备 ...
- SQLSERVER与C#中数据类型的对应关系
SQLSERVER与C#中数据类型的对应关系 ///<summary> ///数据库中与C#中的数据类型对照 ///</summary> ///<paramname=&q ...
- 转载“用USBOOT制作DOS启动盘”
使用软件: Usboot和MaxDOS_5.6s_U盘版. 由于我的U盘容量比较小,暂时只能做DOS启动功能,其它功能如Windows PE,等我以后测试成功后再补充说明. U盘是啥? 读音优盘,可以 ...