17999 Light-bot 模拟 + kmp求循环节
http://acm.scau.edu.cn:8000/uoj/mainMenu.html
17999 Light-bot
时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0
题型: 编程题 语言: 不限定
Description
I (you needn't know
who am "I".) am currently playing a game called
"Light-bot". In the game, the "Light-bot" is controlled
by a program. The
program includes:
(1) The main
procedure. The main procedure is the entrance of the program, same as the
"main" in C/C++.
(2) Sub procedure
#1. Sub procedure No.1.
(3) Sub procedure
#2. Sub procedure No.2.
Note: If a sub
procedure ends, it will return to the command next to it's calling place.
Here, we suggest
that an alphabetical letter stands for an ACTION COMMAND excluding ‘P’ and ‘p’.
So,
"Light-bot" will begin executing from the first command in the main
procedure. Once it meets with a letter ‘P’, it will call sub
procedure #1, while
a letter ‘p’ indicates to call sub procedure #2. The main procedure, procedure
#1 and procedure #2 can call
procedure #1 or
procedure #2 freely. It means that recursive calls are possible.
Now, I just want to
know given a program, what’s the Nth ACTION COMMAND light-bot will execute.
输入格式
The first line of
the input contains an integer T (T <= 1000), indicating there are T cases in
the input file.
For each test case,
the first line is the main procedure. The second one is sub procedure #1 and
the last is sub procedure #2. Each
procedure ends with
a ‘#’ sign, which is not considered a command. The length of a part will not
exceed 10.
And on the next
line, there is one integer n (1 <= n <= 108), indicates the
order I ask. It is GUARANTEED that there must be an ACTION COMMAND
fitting the requirement.
Please see the
example for more details.
输出格式
For each case,
print one line, the ACTION COMMAND letter that fits the description.
输入样例
4
ABCDP#
pEFG#
HIJK#
4
ABCDP#
pEFG#
HIJK#
5
ABCDP#
pEFG#
HIJK#
9
ABCDP#
EFGHP#
#
12
输出样例
D
H
E
H
来源
Lrc_seraph
首先因为其最大的数量是1000(不循环的话)
那么我可以暴力模拟2000次,然后得到一个序列。这个序列的后边肯定是循环的了。
就是XXXXABCABCABC....这样。
然后可以反向kmp一次,求循环节的时候,要从第100项开始,
原因是:
1、第100项开始,求到的循环节长度是一样的,
2、防止AAAA这些假循环节的干扰。
坑了我很久的就是模拟的时候,我模拟到up步,但是取了等号,模拟了UP + 1步。然后一直wa
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
char str[][];
char Ma[];
int len[];
int lenMa;
const int up = + ;
char all[up + ];
int lenall = ;
char sub[up + ];
int lensub = ;
void dfs(int now, int cur) {
if (lenall >= up) return;
for (int i = cur; i <= len[now] && lenall < up; ++i) {
if (str[now][i] == 'P') {
dfs(, );
} else if (str[now][i] == 'p') {
dfs(, );
} else all[++lenall] = str[now][i];
}
}
int tonext[up + ];
void kmp() {
int i = , j = ;
tonext[] = ;
while (i <= lensub) {
if (j == || sub[i] == sub[j]) {
tonext[++i] = ++j;
} else j = tonext[j];
}
}
void work() {
scanf("%s", Ma + );
for (int i = ; i <= ; ++i) {
scanf("%s", str[i] + );
len[i] = strlen(str[i] + );
len[i]--;
}
lenMa = strlen(Ma + );
lenMa--;
lenall = ;
for (int i = ; i <= lenMa && lenall < up; ++i) { //这个up不能去等号
if (Ma[i] == 'P') {
dfs(, );
} else if (Ma[i] == 'p') {
dfs(, );
} else {
all[++lenall] = Ma[i];
}
}
all[lenall + ] = '\0';
int val;
scanf("%d", &val);
if (val <= up) {
printf("%c\n", all[val]);
return;
}
lensub = ;
for (int i = lenall; i >= ; --i) {
sub[++lensub] = all[i];
}
sub[lensub + ] = '\0';
kmp();
// cout << sub + 1 << endl;
int cir = ;
// cout << all + 1 << endl;
for (int i = + ; i <= lensub; ++i) {
if (tonext[i + ] == ) continue;
int t = i - (tonext[i + ] - );
if (i % t == ) {
cir = t;
// cout << i << endl;
break;
}
}
// cout << cir << endl;
if (cir == ) while();
int left = val - up; left %= cir;
if (left == ) left = cir;
int point = lenall - cir + left;
printf("%c\n", all[point]); } int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
17999 Light-bot 模拟 + kmp求循环节的更多相关文章
- hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)
Problem - 3374 KMP求循环节. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html 循环节推导的证明相当 ...
- UVA 12012 Detection of Extraterrestrial(KMP求循环节)
题目描述 E.T. Inc. employs Maryanna as alien signal researcher. To identify possible alien signals and b ...
- 51nod 1126 求递推序列的第N项 思路:递推模拟,求循环节。详细注释
题目: 看起来比较难,范围10^9 O(n)都过不了,但是仅仅是看起来.(虽然我WA了7次 TLE了3次,被自己蠢哭) 我们观察到 0 <= f[i] <= 6 就简单了,就像小学初中学的 ...
- 【HDU 3746】Simpsons’ Hidden Talents(KMP求循环节)
求next数组,(一般有两种,求循环节用的见代码)求出循环节的长度. #include <cstdio> #define N 100005 int n,next[N]; char s[N] ...
- hdu3746 kmp求循环节
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- HDU3746(KMP求循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3746 kmp求循环节
题意就是将所给的字符串变成多个完整的循环(至少两个),然后给出最少需要添加的字符数.
- hdu1358 Period kmp求循环节
链接 http://acm.hdu.edu.cn/showproblem.php?pid=1358 思路 当初shenben学长暑假讲过,当初太笨了,noip前几天才理解过来.. 我也没啥好说的 代码 ...
- (KMP 求循环节)The Minimum Length
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70325#problem/F The Minimum Length Time Limit: ...
随机推荐
- 第三篇:python基础之数据类型与变量
阅读目录 一.变量 二.数据类型 2.1 什么是数据类型及数据类型分类 2.2 标准数据类型: 2.2.1 数字 2.2.1.1 整型: 2.2.1.2 长整型long: 2.2.1.3 布尔bool ...
- Linux--top命令查看系统状态,所有值讲解
Linux系统可以通过top命令查看系统的CPU.内存.运行时间.交换分区.执行的线程等信息.通过top命令可以有效的发现系统的缺陷出在哪里.是内存不够.CPU处理能力不够.IO读写过高. 一.top ...
- 解决 'chromedriver' executable needs to be in PATH.'报错
试了把chromedriver.exe放到chrome安装文件下,python安装文件下,然后把路径配到path里,均无用. 最后是修改函数调用得以解决: from selenium import w ...
- u3d shader学习笔记1
促使我学习SHADER的重要原因是希望深入理解3D渲染的机制,在此基础上可以灵活达到某种效果与性能的平衡,开发出具有良好体验的VR应用. 因为VR应用体验的好坏,直接由游戏的帧率决定,而游戏的帧率则受 ...
- F#周报2019年第20期
新闻 2019年理事会活动 "实用的F#挑战"意见截止日期接近,不要忘记提交博客文章或者其它作品 接口中的默认实现 .NET Core 3.0里的性能增强 使用Try .NET创建 ...
- E20181001-ts
decorate vt. 装饰; 点缀; 粉刷; 授予(某人)勋章 vi. 装饰; 布置;
- oop的三大特点
1.封装性:也称为信息隐藏,就是将一个类的使用和实现分开,只保留部分接口和方法与外部联系,或者说只公开了一些供开发人员使用的方法.于是开发人员只 需要关注这个类如何使用,而不用去关心其具体的实现过程, ...
- Codeforces Round #439 (Div. 2)C - The Intriguing Obsession(简单dp)
传送门 题意 给出三个集合,每个集合的元素数量为a,b,c,现在需要连边,满足集合内元素不可达或最短路为3,求可行方案数 分析 设dp[i][j]为a集合元素为i个,b集合元素为j个的可行方案,易知( ...
- Lightoj 1008【规律】
25 24 23 22 21 10 11 12 13 20 9 8 7 14 19 2 3 6 15 18 1 4 5 16 17 然后把这个转化成: 17 18 19 20 21 10 11 12 ...
- uoj#266. 【清华集训2016】Alice和Bob又在玩游戏(博弈论)
传送门 完了我连sg函数是个啥都快忘了 设\(sg[u]\)为以\(u\)为根节点的子树的\(sg\)函数值,\(rem[u]\)表示\(u\)到根节点的路径删掉之后剩下的游戏的异或值 根节点\(u\ ...