2013 ACM/ICPC Asia Regional Chengdu Online 1004 Minimum palindrome

Minimum palindrome

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 586    Accepted Submission(s): 110


Problem Description
Setting password is very important, especially when you have so many "interesting" things in "F:\TDDOWNLOAD".
We define the safety of a password by a value. First, we find all the substrings of the password. Then we calculate the maximum length of those substrings which, at the meantime, is a palindrome.
A palindrome is a string that will be the same when writing backwards. For example, aba, abba,abcba are all palindromes, but abcab, abab are not.
A substring of S is a continous string cut from S. bcd, cd are the substrings of abcde, but acd,ce are not. Note that abcde is also the substring of abcde.
The smaller the value is, the safer the password will be.
You want to set your password using the first M letters from the alphabet, and its length should be N. Output a password with the smallest value. If there are multiple solutions, output the lexicographically smallest one.
All the letters are lowercase.
 

 

Input
The first line has a number T (T <= 15) , indicating the number of test cases.
For each test case, there is a single line with two integers M and N, as described above.(1 <= M <= 26, 1 <= N <= 10 5)
 

 

Output
For test case X, output "Case #X: " first, then output the best password.
 

 

Sample Input
2
2 2
2 3
 

 

Sample Output
Case #1: ab
Case #2: aab
 
3个及3个以上字母就可以不构成回文串,2个字母的打表找规律
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int T;
10     int cas=1;
11     scanf("%d",&T);
12     while(T--)
13     {
14         int n,m;
15         scanf("%d%d",&n,&m);
16         printf("Case #%d: ",cas++);
17         if(n==1)
18         {
19             while(m--)
20                 putchar('a');
21             putchar(10);
22             continue;
23         }
24         if(n==2)
25         {
26             if(m<=8)
27             {
28                 switch(m)
29                 {
30                 case 1:
31                     printf("a\n"); break;
32                 case 2:
33                     printf("ab\n"); break;
34                 case 3:
35                     printf("aab\n"); break;
36                 case 4:
37                     printf("aabb\n"); break;
38                 case 5:
39                     printf("aaaba\n"); break;
40                 case 6:
41                     printf("aaabab\n"); break;
42                 case 7:
43                     printf("aaababb\n"); break;
44                 case 8:
45                     printf("aaababbb\n"); break;
46                 }
47                 continue ;
48             }
49             else
50             {
51                 printf("aaaababb");
52                 m-=8;
53                 int l=m/6;
54                 for(int i=0;i<l;i++)
55                 {
56                     printf("aababb");
57                 }
58                 l=m%6;
59                 switch(l)
60                 {
61                 case 1:
62                     printf("a\n"); break;
63                 case 2:
64                     printf("aa\n"); break;
65                 case 3:
66                     printf("aaa\n"); break;
67                 case 4:
68                     printf("aaaa\n"); break;
69                 case 5:
70                     printf("aabab\n"); break;
71                 }
72             }
73         }
74         else
75         {
76             for(int i=0;i<m;i++)
77             {
78                 putchar('a'+(i)%3);
79             }
80             putchar(10);
81         }
82     }
83 
84     return 0;
85 }

 

 

 

你可能感兴趣的:(2013 ACM/ICPC Asia Regional Chengdu Online 1004 Minimum palindrome)