Combination Lock

时间:2023-03-09 13:13:23
Combination Lock
时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Combination LockCombination 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 1

For 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。估计是递归开销太大了。分析了一下改成迭代的了。也不知道对不对。。。