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

1002列变位法解密

时间:2016-05-13 18:53:34      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

 1 /*
 2 
 3 Time 124MS
 4 
 5 Memory 1504
 6 
 7 */
 8 #include<iostream>
 9 #include<climits>
10 #include <cstring>
11 #include <cstdlib>
12 #define WORDS "Case #"
13 #define ELSE ":\n"
14 #define MAX 100010
15 using namespace std;
16 
17 static char a[MAX];
18     
19 static char b[MAX];
20 
21 int main()
22 {
23     
24     int N;
25     int unit;
26     cin >> N;
27     
28     for(int i = 0; i < N; i++)
29     {
30         cin.get();
31         
32         gets(a);
33     
34         cin >> unit;
35         
36         cout << "Case #" << i+ 1 << ":" << endl;
37         
38         int length = strlen(a);
39         b[length] = \0;
40         
41         if(unit == 1)
42         {
43             cout << a << endl;
44             continue;
45         }
46         
47         int acount = 0;
48         int bcount = 0;
49         int j = 0;
50         
51          while(acount < length)
52         {
53             if(bcount >= length)
54             {
55                 bcount = ++j;
56             }
57             
58             b[bcount] = a[acount];
59             acount++;
60             bcount += unit;
61         }
62         
63         cout << b << endl;  
64     }
65     
66 }

在做这道题的时候,第一次字符串全用了string所以一直超时,而且找不到原因。

然后用一般的从未解密的数组直接读取正确的顺序需要用到大量的长短控制,虽然最后不会超时,但写起来容易出错。

然后换了一种思路,就如上找到一只后,不是找2在第几个,而是把下一个5写到该有的顺序里面去,这样的话只要控制列数,也不用循环控制,超出长度就回去,整体长度用未解密的数组控制,写起来干净。

如下是第一种思路的实现,因为用了数组记忆多了循环,所以时间会多,完全直接模拟,时间是130多MS。

 1   /*
 2  
 3   Time 337MS
 4  
 5  Memory 1704
 6  
 7   */
 8 #include<iostream>
 9 #include<climits>
10 #include <cstring>
11 #include <cstdlib>
12 #define WORDS "Case #"
13 #define ELSE ":\n"
14 #define MAX 100010
15 using namespace std;
16 
17 static int record[MAX];
18 static char a[MAX];
19 int main()
20 {
21 
22     int length;
23     int Index;
24     
25     int rows;
26     int lastcolumns;
27     int columns;
28     
29     
30     cin >> Index;
31 
32     for(int n = 0; n < Index; n++)
33     {    
34         cin.get();
35         gets(a);
36         cin >> columns; 
37         cout << WORDS << n + 1 << ELSE;
38         
39         length = strlen(a);
40         rows = length / columns;
41         if(length % columns != 0)
42         {
43             lastcolumns = length % columns;
44             rows++;
45         }
46         else
47         {
48             lastcolumns = columns;
49         }
50         
51         for(int i = 0; i < columns; i++)
52         {
53             if(i <     lastcolumns)
54             {
55                 record[i] = rows;
56             }
57             else
58             {
59                 record[i] = rows - 1;
60             }                
61         }
62     
63         
64         int sta;
65         
66         for(int i = 0; i < rows; i++)
67         {
68             sta = i;
69             if(    i == rows - 1)
70             {
71                 columns = lastcolumns;
72             }
73             for(int j = 0; j < columns;j++)
74             {
75                 cout << a[sta];
76                 sta += record[j];
77             }
78             
79         }
80         cout << endl;
81     }
82 }

 

1002列变位法解密

标签:

原文地址:http://www.cnblogs.com/202652TJ/p/5490369.html

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