Combination Lock
描述
Finally, you come to the interview room. You know that a Microsoft interviewer is in the room though the door is locked. There is a combination lock on the door. There are N rotators on the lock, each consists of 26 alphabetic characters, namely, 'A'-'Z'. You need to unlock the door to meet the interviewer inside. There is a note besides the lock, which shows the steps to unlock it.
Note: There are M steps totally; each step is one of the four kinds of operations shown below:
Type1: CMD 1 i j X: (i and j are integers, 1 <= i <= j <= N; X is a character, within 'A'-'Z')
This is a sequence operation: turn the ith to the jth rotators to character X (the left most rotator is defined as the 1st rotator)
For example: ABCDEFG => CMD 1 2 3 Z => AZZDEFG
Type2: CMD 2 i j K: (i, j, and K are all integers, 1 <= i <= j <= N)
This is a sequence operation: turn the ith to the jth rotators up K times ( if character A is turned up once, it is B; if Z is turned up once, it is A now. )
For example: ABCDEFG => CMD 2 2 3 1 => ACDDEFG
Type3: CMD 3 K: (K is an integer, 1 <= K <= N)
This is a concatenation operation: move the K leftmost rotators to the rightmost end.
For example: ABCDEFG => CMD 3 3 => DEFGABC
Type4: CMD 4 i j(i, j are integers, 1 <= i <= j <= N):
This is a recursive operation, which means:
If i > j:
Do Nothing
Else:
CMD 4 i+1 j
CMD 2 i j 1For example: ABCDEFG => CMD 4 2 3 => ACEDEFG
输入
1st line: 2 integers, N, M ( 1 <= N <= 50000, 1 <= M <= 50000 )
2nd line: a string of N characters, standing for the original status of the lock.
3rd ~ (3+M-1)th lines: each line contains a string, representing one step.
输出
One line of N characters, showing the final status of the lock.
提示
Come on! You need to do these operations as fast as possible.
- 样例输入
-
7 4
ABCDEFG
CMD 1 2 5 C
CMD 2 3 7 4
CMD 3 3
CMD 4 1 7 - 样例输出
-
HIMOFIN
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <queue> using namespace std; void cmd1(string &str, int i, int j, char c) {
for (int m = i; m <= j; ++m) {
str[m - ] = c;
}
} void cmd2(string &str, int i, int j, int k) {
for (int m = i; m <= j; ++m) {
int v = (str[m - ] - 'A' + k) % ;
str[m - ] = 'A' + v;
}
} void reverse(string &str, int i, int j) {
while (i < j) {
swap(str[i], str[j]);
i++, j--;
}
} void cmd3(string &str, int k) {
reverse(str, , k - );
reverse(str, k, str.length() - );
reverse(str, , str.length() - );
} void cmd4(string &str, int i, int j) {
if (i > j) return;
//cmd4(str, i + 1, j);
//cmd2(str, i, j, 1);
for (int m = i; m <= j; ++m) {
int v = (str[m - ] - 'A' + m - i + ) % ;
str[m - ] = 'A' + v;
}
} int main(int argc, char** argv) {
int n, m;
cin >> n >> m;
string input;
cin >> input; for (int step = ; step < m; ++step) {
string cmd;
int type, i, j, k;
char c;
cin >> cmd >> type;
if (type == ) {
cin >> i >> j >> c;
cmd1(input, i, j, c);
} else if (type == ) {
cin >> i >> j >> k;
cmd2(input, i, j, k);
} else if (type == ) {
cin >> k;
cmd3(input, k);
} else if (type == ) {
cin >> i >> j;
cmd4(input, i, j);
}
//cout << input << endl;
} cout << input << endl;
return ;
}
老是TLE。估计是递归开销太大了。分析了一下改成迭代的了。也不知道对不对。。。
Combination Lock的更多相关文章
- hihocoder #1058 Combination Lock
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a ...
- 贪心 Codeforces Round #301 (Div. 2) A. Combination Lock
题目传送门 /* 贪心水题:累加到目标数字的距离,两头找取最小值 */ #include <cstdio> #include <iostream> #include <a ...
- Codeforces Round #301 (Div. 2) A. Combination Lock 暴力
A. Combination Lock Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/p ...
- Hiho----微软笔试题《Combination Lock》
Combination Lock 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You ...
- CF #301 A :Combination Lock(简单循环)
A :Combination Lock 题意就是有一个密码箱,密码是n位数,现在有一个当前箱子上显示密码A和正确密码B,求有A到B一共至少需要滚动几次: 简单循环:
- hihocoder-第六十一周 Combination Lock
题目1 : Combination Lock 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview roo ...
- A - Combination Lock
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Scroog ...
- HDU 3104 Combination Lock(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3104 Problem Description A combination lock consists ...
- 洛谷 P2693 [USACO1.3]号码锁 Combination Lock
P2693 [USACO1.3]号码锁 Combination Lock 题目描述 农夫约翰的奶牛不停地从他的农场中逃出来,导致了很多损害.为了防止它们再逃出来,他买了一只很大的号码锁以防止奶牛们打开 ...
随机推荐
- SOLID面向对象的五个设计原则,留空待学习。
SOLID面向对象的五个设计原则对于开发人员非常重要,其身影在任何大中型软件项目中随处可见,建议必须掌握并灵活应用.此五原则分别为: 单一职责原则(Single Resposibility ...
- 选项卡 tab切换
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- maven工程下 读取resource下配置文件
http://blog.csdn.net/xu511739113/article/details/52440982
- Open CV 图像显示(1)
演示:读入一张图片,并显示 #include "stdafx.h" #include <opencv2/core/core.hpp> #include ...
- Silverlight
http://kb.cnblogs.com/zt/silverlight/ http://www.cnblogs.com/gnielee/archive/2010/01/15/silverlight- ...
- BZOJ3257 : 树的难题
设$f[x][i][j]$表示以$x$为根的子树,与$x$连通部分有$i$个黑点,$j$个白点,不联通部分都是均衡的最小代价.若$i>1$,则视作$1$:若$j>2$,则视作$2$. 然后 ...
- 原生js自动触发事件
熟悉jquery的童鞋都知道在jq中有一个方法可以自动触发事件,那就是trigger(),那么通过原生js又怎么模拟触发呢? js中添加一个主动触发事件的方法有dispatch.该方法能模拟用户行为, ...
- 【BZOJ】2286: [Sdoi2011消耗战
http://www.lydsy.com/JudgeOnline/problem.php?id=2286 题意:n个点的边加权树,m个询问,每次询问给出的k个点与结点1分离的最小代价.(n<=2 ...
- InterBase数据库迁移到MySQL(恢复备份)
我拿到的是InterBase导出的“.gbk”后缀的数据库备份文件,目标是可以通过命令行的方式导入到指定的数据库中,在这个脚本中我使用了InterBase数据库中自带的“gbak”命令行来进行操作. ...
- 【Oracle】同义词问题
优点: 1.节省数据库空间,多用户可以操作同一张表: 2.扩展的数据库的使用范围,能够在不同的数据库用户之间实现无缝交互: 3.利用Database Link.创建同义词可以实现不同数据库服务器之间的 ...