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因此一直超時,並且找不到緣由。ios

而後用通常的從未解密的數組直接讀取正確的順序須要用到大量的長短控制,雖然最後不會超時,但寫起來容易出錯。數組

而後換了一種思路,就如上找到一隻後,不是找2在第幾個,而是把下一個5寫到該有的順序裏面去,這樣的話只要控制列數,也不用循環控制,超出長度就回去,總體長度用未解密的數組控制,寫起來乾淨。spa

以下是第一種思路的實現,由於用了數組記憶多了循環,因此時間會多,徹底直接模擬,時間是130多MS。code

 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 }
相關文章
相關標籤/搜索