现在的位置: 首页 > 综合 > 正文

成都赛区网赛D Minimum palindrome

2013年09月16日 ⁄ 综合 ⁄ 共 2928字 ⁄ 字号 评论关闭

题目:

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 <= 105)
 
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

 

题解:

这道题的题面意思是对一个长为N的字符串有一个value定义,就是最长回文字串的长度,value越大,密码的安全性越低。所以我们可以试着探索下规律:

当m = 1时,我就不解释了,直接全部写aaaaaaaaaaaaa就行了=。=

当m >= 3时,对一个形如abcabcabc...的字符串,显然他的value值是1,所以这种情况的话,直接输出abc循环就行了。

当m = 2时,我们一个一个的分析。

 

m 结果 value
2 ab 1
3 aab 2
4 aabb 2
5 aaaba 3
6 aaabab 3
7 aaababb 3
8 aaababbb 3
9 aaababbaa 4
10 aaaababbaa 4
11 aaaababbaaa 4
12 aaaababbaaaa 4
13 aaaababbaabab 4
14 aaaababbaababb 4
15 aaaababbaababba 4
16 aaaababbaababbaa 4
17 aaaababbaababbaaa

4

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

规律已经有了。

代码样例

/****
	*@Polo-shen
	*
	*/
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <vector>
#include <fstream>

using namespace std;
typedef long long int64;

#define DBG 0
#define ShowLine DBG && cout<<__LINE__<<">>| "
#define dout DBG && cout<<__LINE__<<">>| "
#define write(x) #x" = "<<(x)<<", "
#define awrite(array,num) #array"["<<num<<"]="<<array[num]<<" "

#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif

#ifndef max
#define max(x,y) ((x) > (y) ? (x) : (y))
#endif
int T,tt=1;
void solve(int m,int n){
    printf("Case #%d: ",tt);tt++;
    if (m==1){
        for (int i=0;i<n;i++){
            printf("a");
        }
        printf("\n");
    }
    else if (m>=3){
        int no=0,tmpm=3;
        for (int i=0;i<n;i++){
            no=i%tmpm;
            char ch=(char)('a'+no);
            printf("%c",ch);
        }
        printf("\n");
    }
    else {
        switch(n){
            case 1:{
                printf("a\n");
                break;
            }
            case 2:{
                printf("ab\n");
                break;
            }
            case 3:{
                printf("aab\n");
                break;
            }
            case 4:{
                printf("aabb\n");
                break;
            }
            case 5:{
                printf("aaaba\n");
                break;
            }
            case 6:{
                printf("aaabab\n");
                break;
            }
            case 7:{
                printf("aaababb\n");
                break;
            }
            case 8:{
                printf("aaababbb\n");
                break;
            }
            default:{
                printf("aaaababb");
                int tmpn=n-8;
                while (tmpn>=6){
                    printf("aababb");
                    tmpn-=6;
                }
                switch (tmpn){
                    case 0:{
                        printf("\n");break;
                    }
                    case 1:{
                        printf("a\n");break;
                    }
                    case 2:{
                        printf("aa\n");break;
                    }
                    case 3:{
                        printf("aaa\n");break;
                    }
                    case 4:{
                        printf("aaaa\n");break;
                    }
                    case 5:{
                        printf("aabab\n");break;
                    }
                    default:break;
                }
            }
        }
    }
}

int main(){
    cin>>T;while (T--){
        int n,m;
        cin>>m>>n;
        solve(m,n);
    }
    return 0;
}

 

 

 

抱歉!评论已关闭.