HDU 4731 Minimum palindrome (2013成都网络赛,找规律构造)

Minimum palindrome

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

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  

 

Source
2013 ACM/ICPC Asia Regional Chengdu Online  

 

Recommend liuyiding  

 

找规律。

 

当m=1时,直接输出n个a

当m>=3时,输出abcabcabc….

 

当m=2时,先暴力打出1~20的表。然后找规律发现循环

 1 /* ***********************************************
 2 Author        :kuangbin
 3 Created Time  :2013/9/14 星期六 13:21:24
 4 File Name     :2013成都网络赛\1004.cpp
 5 ************************************************ */
 6 
 7 #pragma comment(linker, "/STACK:1024000000,1024000000")
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <iostream>
11 #include <algorithm>
12 #include <vector>
13 #include <queue>
14 #include <set>
15 #include <map>
16 #include <string>
17 #include <math.h>
18 #include <stdlib.h>
19 #include <time.h>
20 using namespace std;
21 
22 char str[10000];
23 int main()
24 {
25     //freopen("in.txt","r",stdin);
26     //freopen("out.txt","w",stdout);
27     int T;
28     int m,n;
29     int iCase = 0;
30     scanf("%d",&T);
31     while(T--)
32     {
33         scanf("%d%d",&m,&n);
34         iCase++;
35         printf("Case #%d: ",iCase);
36         if(m == 1)
37         {
38             for(int i = 0;i < n;i++)printf("a");
39             printf("\n");
40             continue;
41         }
42         if(m >= 3)
43         {
44             int index = 0;
45             for(int i = 0;i < n;i++)
46             {
47                 printf("%c",index + 'a');
48                 index = (index + 1)%3;
49             }
50             printf("\n");
51             continue;
52         }
53         if(n == 1)printf("a");
54         else if(n == 2)printf("ab");
55         else if(n == 3)printf("aab");
56         else if(n == 4)printf("aabb");
57         else if(n == 5)printf("aaaba");
58         else if(n == 6)printf("aaabab");
59         else if(n == 7)printf("aaababb");
60         else if(n == 8)printf("aaababbb");
61         else if(n == 9)printf("aaaababba");
62         else
63         {
64             printf("aaaa");
65             n -= 4;
66             while(n >= 6)
67             {
68                 printf("babbaa");
69                 n -= 6;
70             }
71             if(n == 1)printf("a");
72             else if(n == 2)printf("aa");
73             else if(n == 3)printf("bab");
74             else if(n == 4)printf("babb");
75             else if(n == 5)printf("babba");
76         }
77         printf("\n");
78     }
79     return 0;
80 }

 

 

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3321994.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞