1002列变位法解密

 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 }

 

转载于:https://www.cnblogs.com/202652TJ/p/5490369.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值