首页 > 代码库 > HDU 4731 Minimum palindrome 打表找规律

HDU 4731 Minimum palindrome 打表找规律

题目链接

虽然想到了可能有规律,但是比赛的时候没有去仔细推敲。

暴力打表找出可以得到对应的长度n和对应字符集m所对应的答案

 

10 1
 1 1 a
 2 2 aa
 3 3 aaa
 4 4 aaaa
 5 5 aaaaa
 6 6 aaaaaa
 7 7 aaaaaaa
 8 8 aaaaaaaa
 9 9 aaaaaaaaa
10 10 aaaaaaaaaa
20 2
 1 1 a
 2 1 ab
 3 2 aab
 4 2 aabb
 5 3 aaaba
 6 3 aaabab
 7 3 aaababb
 8 3 aaababbb
 9 4 aaaababba
10 4 aaaababbaa
11 4 aaaababbaaa
12 4 aaaababbaaaa
13 4 aaaababbaabab
14 4 aaaababbaababb
15 4 aaaababbaababba
16 4 aaaababbaababbaa
17 4 aaaababbaababbaaa
18 4 aaaababbaababbaaaa
19 4 aaaababbaababbaabab
20 4 aaaababbaababbaababb
10 3
 1 1 a
 2 1 ab
 3 1 abc
 4 1 abca
 5 1 abcab
 6 1 abcabc
 7 1 abcabca
 8 1 abcabcab
 9 1 abcabcabc
10 1 abcabcabca
10 4
 1 1 a
 2 1 ab
 3 1 abc
 4 1 abca
 5 1 abcab
 6 1 abcabc
 7 1 abcabca
 8 1 abcabcab
 9 1 abcabcabc
10 1 abcabcabca

 

 

然后很容易发现规律,m=1和m=3的时,循环节分别是a和abc,m=2的时候,从长度为9的时候开始循环,循环节为babbaa,然后前面8个打表就行了。

<style>pre { font-family: monospace; color: #ffffff; background-color: #000000 } body { font-family: monospace; color: #ffffff; background-color: #000000 } .lnr { color: #ffff00 } .Statement { color: #ffff00 } .Special { color: #ffd7d7 } .Type { color: #87ffaf } .Constant { color: #ff40ff } .PreProc { color: #5fd7ff }</style>
技术分享
 1 #include <stdio.h>
 2 const char ans[][9] = {"a", "ab", "aab", "aabb", "aaaba", "aaabab", "aaababb", "aaababbb"};
 3 int main() {
 4     int T;
 5     scanf("%d", &T);
 6     for (int Tc = 1; Tc <= T; ++Tc) {
 7         int n, m;
 8         scanf("%d%d", &m, &n);
 9         printf("Case #%d: ", Tc);
10         if (m == 1) {
11             for (int i = 0; i < n; ++i)
12                 putchar(‘a‘);
13         }
14         else if (m >= 3) {
15             for (int i = 0; i < n; ++i)
16                 putchar("abc"[i % 3]);
17         }
18         else {
19             if (n <= 8)
20                 printf("%s", ans[n - 1]);
21             else {
22                 printf("aaaa");
23                 for (int i = 4; i < n; ++i)
24                     putchar("babbaa"[(i + 2) % 6]);
25             }
26         }
27         putchar(‘\n‘);
28     }
29     return 0;
30 }
View Code

 

HDU 4731 Minimum palindrome 打表找规律