2013成都網路賽1004題HDU 4731Minimum palindrome (思維題目)

果7發表於2013-09-14

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

Source
 

題目大意:給你一個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


相關文章