2013成都网络赛1004题HDU 4731Minimum palindrome (思维题目)

Minimum palindrome

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


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
 

题目大意: 给你一个m,n.用a,b,c,d,e一共m种字母组成长度为n的字符串中的最长回文子串长度最短。如果有多组长度一样,输出字典序最小的。开始吉吉以为是搜索,然后就开始写搜索,然后用了manacher匹配算法。记得aaaababbaababbaa这样的在开始组队赛的时候遇到过类似的。

 解题思路:如果m为1,直接输出。如果m>=3直接输出abcabc这样的可以使得最长回文长度为1.现在主要是讨论m为2的情况,然后就开始枚举了,开始找规律。a,ab,aab,aabb,aaaba,aaabab,aaababb,aaababbb。这是1~8的情况,发现当n=9的时候回文长度至少得为4.然后就先输出aaaa,然后找使得回文长度最长为4且字典序尽可能小的。发现是babbaababbaa.....如此便得到循环串babbaa.后面就好处理了。具体实现见代码。

 题目地址:Minimum palindrome

AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;

char p[8][10]={"a","ab","aab","aabb","aaaba","aaabab","aaababb","aaababbb"};
char a[6]={'b','a','b','b','a','a'};

int main()
{
    int tes,i;
    int cas=0;
    scanf("%d",&tes);
    while(tes--)
    {
        int m,n;
        printf("Case #%d: ",++cas);
        scanf("%d%d",&m,&n);
        if(n==1)
            cout<<"a"<<endl;
        else if(m==1)
        {
            for(i=0;i<n;i++)
                cout<<"a";
            cout<<endl;
        }
        else if(m==2)
        {
            if(n<=8)  //回文长度最多为3
                cout<<p[n-1]<<endl;
            else  //回文长度为4
            {
                cout<<"aaaa";
                n-=4;
                int tmp=n/6;
                for(i=0;i<tmp;i++)
                    cout<<"babbaa";
                tmp=n-tmp*6;
                for(i=0;i<tmp;i++)
                    cout<<a[i];
                cout<<endl;
            }
        }
        else
        {
            int tmp=n/3;
            for(i=0;i<tmp;i++)
                cout<<"abc";
            if(n-tmp*3==2)
                cout<<"ab";
            else if(n-tmp*3==1)
                cout<<"a";
            cout<<endl;
        }
    }
    return 0;
}

//15MS 272K


你可能感兴趣的:(思维,回文串)