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

字符串处理 2015百度之星资格赛 1002 列变位法解密

时间:2015-06-01 20:06:56      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

 1 /*
 2     字符串处理:要求解码,然而二维数组开不下,可以直接输出
 3     只要在余数的地方判断一下就行了,vector的效率不高
 4     另外:感觉麻烦的地方应该纸上写写就清楚了
 5 */
 6 #include <cstdio>
 7 #include <iostream>
 8 #include <algorithm>
 9 #include <cstring>
10 #include <string>
11 #include <cmath>
12 using namespace std;
13 
14 const int MAXM = 1e6 + 10;
15 const int INF = 0x3f3f3f3f;
16 char s[MAXM];
17 char ans[MAXM];
18 
19 int main(void)        //2015百度之星资格赛 1002 列变位法解密
20 {
21     int n, m, k, cas = 0;
22     int T;
23     scanf ("%d", &T);    getchar ();
24     while (T--)
25     {
26         gets (s);
27         scanf ("%d", &m);
28         int len = strlen (s);
29         n = len / m;
30         k = len % m;
31 
32         printf ("Case #%d:\n", ++cas);
33         int i = 0, j, l = 0, cnt;
34         while (i < len)
35         {
36             j = i;    cnt = 0;
37             while (j < len)
38             {
39                 printf ("%c", s[j]);    ++l;
40                 if (cnt < k)    {j += (n + 1); ++cnt;}
41                 else    j += n;
42                 if (l == len)    break;
43             }
44             ++i;    if (l == len)    break;
45         }
46 
47         puts ("");    getchar ();
48     }
49 
50     return 0;
51 }
52 
53 
54 /*
55 4
56 159263748
57 4
58 Hw doeetrrlloellc adoomm!,my  e w
59 7
60 Toodming is best
61 16
62 sokaisan
63 1
64 */
技术分享
 1 /*
 2     800多MS
 3 */
 4 #include <cstdio>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <cstring>
 8 #include <string>
 9 #include <cmath>
10 #include <vector>
11 using namespace std;
12 
13 const int MAXM = 1e5 + 10;
14 const int INF = 0x3f3f3f3f;
15 char s[MAXM];
16 vector<char> ss[MAXM];
17 
18 int main(void)        //2015百度之星资格赛 1002 列变位法解密
19 {
20     int n, m, k, cas = 0;
21     int T;
22     scanf ("%d", &T);    getchar ();
23     while (T--)
24     {
25         gets (s);
26         //getline (cin, s);
27         scanf ("%d", &m);
28         int len = strlen (s);
29         n = len / m;
30         k = len % m;
31 
32         for (int i=0; i<=len; ++i)    ss[i].clear ();
33 
34         int l = 0;    int t = k;
35         for (int j=0; j<m; ++j)
36         {
37             for (int i=0; i<n; ++i)
38             {
39                 ss[i].push_back (s[l++]);
40                 //ans[i][j] = s[l++];
41             }
42             if (t > 0)    ss[n].push_back (s[l++]);
43             t--;
44         }
45 
46         printf ("Case #%d:\n", ++cas);
47         for (int i=0; i<n; ++i)
48         {
49             for (int j=0; j<m; ++j)
50             {
51                 cout << ss[i][j];
52                 //printf ("%c", ans[i][j]);
53             }
54         }
55         for (int i=0; i<k; ++i)
56             cout << ss[n][i];
57             //printf ("%c", ans[n][i]);
58         puts ("");    getchar ();
59     }
60 
61     return 0;
62 }
63 
64 
65 /*
66 4
67 159263748
68 4
69 Hw doeetrrlloellc adoomm!,my  e w
70 7
71 Toodming is best
72 16
73 sokaisan
74 1
75 */
vector

 

字符串处理 2015百度之星资格赛 1002 列变位法解密

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4544629.html

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