码迷,mamicode.com
首页 > 其他好文 > 详细

Combination Lock

时间:2014-10-20 00:32:35      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

bubuko.com,布布扣bubuko.com,布布扣

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
 1 #include <iostream>
 2 #include <stdlib.h>
 3 #include <cstdio>
 4 #include <string>
 5 #include <vector>
 6 #include <stack>
 7 #include <map>
 8 #include <queue>
 9 
10 using namespace std;
11 
12 void cmd1(string &str, int i, int j, char c) {
13     for (int m = i; m <= j; ++m) {
14         str[m - 1] = c;
15     }
16 }
17 
18 void cmd2(string &str, int i, int j, int k) {
19     for (int m = i; m <= j; ++m) {
20         int v = (str[m - 1] - A + k) % 26;
21         str[m - 1] = A + v;
22     }
23 }
24 
25 void reverse(string &str, int i, int j) {
26     while (i < j) {
27         swap(str[i], str[j]);
28         i++, j--;
29     }
30 }
31 
32 void cmd3(string &str, int k) {
33     reverse(str, 0, k - 1);
34     reverse(str, k, str.length() - 1);
35     reverse(str, 0, str.length() - 1);
36 }
37 
38 void cmd4(string &str, int i, int j) {
39     if (i > j) return;
40     //cmd4(str, i + 1, j);
41     //cmd2(str, i, j, 1);
42     for (int m = i; m <= j; ++m) {
43         int v = (str[m - 1] - A + m - i + 1) % 26;
44         str[m - 1] = A + v;
45     }
46 }
47 
48 int main(int argc, char** argv) {
49     int n, m;
50     cin >> n >> m;
51     string input;
52     cin >> input;
53 
54     for (int step = 0; step < m; ++step) {
55         string cmd; 
56         int type, i, j, k; 
57         char c;
58         cin >> cmd >> type;
59         if (type == 1) {
60             cin >> i >> j >> c;
61             cmd1(input, i, j, c);
62         } else if (type == 2) {
63             cin >> i >> j >> k;
64             cmd2(input, i, j, k);
65         } else if (type == 3) {
66             cin >> k;
67             cmd3(input, k);
68         } else if (type == 4) {
69             cin >> i >> j;
70             cmd4(input, i, j);
71         }
72     //cout << input << endl;
73     }
74 
75     cout << input << endl;
76     return 0;
77 }

老是TLE。估计是递归开销太大了。分析了一下改成迭代的了。也不知道对不对。。。

Combination Lock

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/linyx/p/4036175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!