您的位置:首页 > 其它

UVa_10785 - The Mad Numerologist

2014-06-24 19:35 465 查看
题意:

对26个大写字母,每个字母都赋一个权值,比如A,J,S权值为1;然后求一个字符串,满足权值和最小(辅音字母和元音字母),字母序列最小(即按照字典序排序)。

思路:

1. 先选择出满足题目要求的元音字母和辅音字母,使权值最小,元音字母序列存储在str[0][220]数组中,辅音字母存储在str[1][220]数组中;

2. 对两个数组排序,得到lexicographically order;

3.两个数组交叉输出,即可得到the numerologists first priority is to keep the vowel and consonant value minimum and then to make the name lexicographically smallest(辅音字母和元音字母权值和最小,字母序列最小)的结果。

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

char vowel[]={'A','U','E','O','I'};
char consonant[]={'J','S','B','K','T','C','L','D','M','V','N','W','F','X','G','P','Y','H','Q','Z','R'};
int cnt[26];
char str[2][220];

void Init()
{
for(int i=0;i<26;i++){
if(i==0||i==4||i==8||i==14||i==20) cnt[i]=21;
else cnt[i]=5;
}
}
char Select(int n)
{
if(n%2)
{
for(int i=0;i<5;i++){
if(cnt[vowel[i]-'A']>0) {cnt[vowel[i]-'A']--;return vowel[i];}
}
}
else
{
for(int i=0;i<21;i++){
if(cnt[consonant[i]-'A']>0) {cnt[consonant[i]-'A']--;return consonant[i];}
}
}
}
int main()
{
int N,n,f1,f2;
scanf("%d",&N);
for(int i=1;i<=N;i++)
{
Init();
f1=f2=0;
memset(str[0],'\0',sizeof(str[0]));
memset(str[1],'\0',sizeof(str[1]));

scanf("%d",&n);
for(int j=0;j<n;j++){
if((j+1)%2) str[0][f1++]=Select(j+1);
else str[1][f2++]=Select(j+1);
}
sort(str[0],str[0]+f1);
sort(str[1],str[1]+f2);
printf("Case %d: ",i);
for(int j=0,f1=0,f2=0;j<n;j++){
if((j+1)%2) putchar(str[0][f1++]);
else putchar(str[1][f2++]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: