2013 ACM/ICPC Asia Regional Chengdu Online HDU 4731 Minimum palindrome(规律)

来源:互联网 发布:美国数据挖掘前景 编辑:程序博客网 时间:2024/06/06 15:46

Minimum palindrome

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


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
22 22 3
 

Sample Output
Case #1: abCase #2: aab
 

Source
2013 ACM/ICPC Asia Regional Chengdu Online
 

Recommend
liuyiding

暴力跑一下m==2的情况:

#include<iostream>#include<cstdio>#include<cstring>#include<string>using namespace std;int d;int re;string s;string ans;int cal(string s){int res=1;int lens=s.length();for(int i=0;i<lens;i++){for(int j=i+1;j<lens;j++){int flag=true;for(int k=i;k<=j;k++)if(s[k]!=s[i+j-k]){flag=false;break;}if(flag==true)res=max(res,j-i+1);}}return res;}void dfs(int n){if(n==d){//cout<<s<<endl;int tmp=cal(s);if(tmp<re){re=tmp;ans=s;}return;}s+='a';dfs(n+1);s.erase(s.end()-1);s+='b';dfs(n+1);s.erase(s.end()-1);}int main(int argc,char **argv){for(int i=1;i<=20;i++){re=10000;d=i;dfs(0);cout<<ans<<endl;}}

然后,就找到规律了。

#include<iostream>#include<cstdio>#include<cstring>using namespace std;char px[3]={'a','b','c'};int main(int argc,char **argv){int T;scanf("%d",&T);for(int idx=1;idx<=T;idx++){int n,m;scanf("%d%d",&m,&n);printf("Case #%d: ",idx);if(m>2){for(int i=0;i<n;i++){printf("%c",px[i%3]);}}else if(m==2){if(n==1)printf("a");if(n==2)printf("ab");if(n==3)printf("aab");if(n==4)printf("aabb");if(n==5)printf("aaaba");if(n==6)printf("aaabab");if(n==7)printf("aaababb");if(n==8)printf("aaababbb");if(n>=9){printf("aa");n-=2;int k=n/6;int r=n-k*6;for(int i=1;i<=k;i++)printf("aababb");if(r==1)printf("a");if(r==2)printf("aa");if(r==3)printf("aaa");if(r==4)printf("aaaa");if(r==5)printf("aabab");}}else{for(int i=1;i<=n;i++)printf("a");}printf("\n");}}