Minimum palindrome
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 309 Accepted Submission(s): 150
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
一看题目觉得挺难得,再看清楚的话,一道找规律的水题:
当M=1时,很简单,输出N个a就行了;
当M=2时,前8个没规律,后面的有规律的;
当M=3时,”abc“这个字符串一直排到N个就行了;
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h> using namespace std; int main()
{
int t,i;
cin>>t;
int k=;
char a[];
while(t--)
{
int n,m;
cin>>n>>m;
cout<<"Case #"<<k++<<": ";
if(n==)
{
for(i=; i<m; i++)
cout<<'a';
cout<<endl;
}
else if(n>)
{
for(i=; i<m; i++)
cout<<(char)('a'+i%);
cout<<endl;
}
else
{
char b[][]= {"a",
"ab",
"aab",
"aabb",
"aaaba",
"aaabab",
"aaababb",
"aaababbb"
};
if(m<=)
{
cout<<b[m-]<<endl;
}
else
{
cout<<"aa";
m--;
m--;
for(i=;i<m/;i++)
cout<<"aababb";
if(m%==)
cout<<'a';
else
if(m%==)
cout<<"aa";
else
if(m%==)
cout<<"aaa";
if(m%==)
cout<<"aaaa";
if(m%==)
cout<<"aabab";
cout<<endl;
}
}
}
}