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

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

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");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值