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求循环节的更多相关文章

  1. hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)

    Problem - 3374   KMP求循环节. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html   循环节推导的证明相当 ...

  2. UVA 12012 Detection of Extraterrestrial(KMP求循环节)

    题目描述 E.T. Inc. employs Maryanna as alien signal researcher. To identify possible alien signals and b ...

  3. 51nod 1126 求递推序列的第N项 思路:递推模拟,求循环节。详细注释

    题目: 看起来比较难,范围10^9 O(n)都过不了,但是仅仅是看起来.(虽然我WA了7次 TLE了3次,被自己蠢哭) 我们观察到 0 <= f[i] <= 6 就简单了,就像小学初中学的 ...

  4. 【HDU 3746】Simpsons’ Hidden Talents(KMP求循环节)

    求next数组,(一般有两种,求循环节用的见代码)求出循环节的长度. #include <cstdio> #define N 100005 int n,next[N]; char s[N] ...

  5. hdu3746 kmp求循环节

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

  6. HDU3746(KMP求循环节)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. hdu 3746 kmp求循环节

    题意就是将所给的字符串变成多个完整的循环(至少两个),然后给出最少需要添加的字符数.

  8. hdu1358 Period kmp求循环节

    链接 http://acm.hdu.edu.cn/showproblem.php?pid=1358 思路 当初shenben学长暑假讲过,当初太笨了,noip前几天才理解过来.. 我也没啥好说的 代码 ...

  9. (KMP 求循环节)The Minimum Length

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70325#problem/F The Minimum Length Time Limit: ...

随机推荐

  1. DDD领域驱动之干货(二)

       基于仓储的实现 1.前言:本着第一节写的有些糊涂,主要是自己喜欢实干,不太喜欢用文字表述,就这样吧.下面切入正题. 博客园里面有很多的大佬,我这里就不一一解释概览,有兴趣的朋友可以去看大佬们写的 ...

  2. Rsync+Sersync同步

    Rsync+Sersync同步特点: (1):sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或某一个目录的名字:(2):rsync在同步的时候,只同步发生变化的这 ...

  3. regular

    regular.py import re # . # 只能匹配一个字母,而不是2个或0个 # \ # 转义 # 'abc\\.com' r'abc\.com' # 字符集[] # 匹配他所包括的任意字 ...

  4. C++之面向对象初探----对象管理模型(关键是this指针)

    前言 c++对象模型可以概括为以下2部分 1.语言中直接支持面向对象程序员设计部分,主要涉及如构造函数.析构函数.虚函数.继承(单继承.多继承.虚继承).多态等待. 2.对于各种支持的底层实现机制 在 ...

  5. bzoj 4712 洪水 —— 动态DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4712 设 f[x] = min(∑f[u] , a[x]),ls = ∑f[lson] 矩阵 ...

  6. java+poi实现word转html显示

    直入正题,需求为页面预览word文档,用的是poi3.8,以下代码支持表格.图片,不支持分页,只支持doc,不支持docx: 1.导jar包 2.java文件 /** * */ import java ...

  7. 注册页面Page的内置属性以及函数 路由 模块化

    Page.prototype.route  route字段可以获取到当前页面的路径 Page.prototype.setData() setData函数用于将数据从逻辑层发送到视图层,同时改变对应的t ...

  8. jq之鼠标事件

    以防自己忘记,最重要的是hover效果的 鼠标事件是在用户移动鼠标光标或者使用任意鼠标键点击时触发的.   (1):click事件:click事件于用户在元素敲击鼠标左键,并在相同元素上松开左键时触发 ...

  9. CodeForces 1091G. New Year and the Factorisation Collaboration

    题目简述:若你获得“超能力”:固定$n$,对任意$a$,可以快速求出$x \in [0, n)$(若存在),使得$x^2 \equiv a \pmod n$,若存在多个$x$满足条件,则返回其中一个( ...

  10. vector 大小

    vector定义以后就最好确定大小resize(),否则在vector析构时可能出现 "double free or corruption"这样的错误