Uva 10785 - The Mad Numerologist
The Mad Numerologist |
Time limit: 3.000 seconds
Numerology is a science that is used by many people to find out a mans personality, sole purpose of life, desires to experience etc. Some calculations of numerology are very complex, while others are quite simple. You can sit alone at home and do these easy calculations without taking any ones help. However in this problem you wont be asked to find the value of your name.
To find the value of a name modern numerologists have assigned values to all the letters of English Alphabet. The table on the left shows the numerical values of all letters of English alphabets. Five letters A, E, I, O, U are vowels. Rests of the letters are consonant. In this table all letters in column 1 have value 1, all letters in column 2 have value 2 and so on. So T has value 2, F has value 6, R has value 9, O has value 6 etc. When calculating the value of a particular name the consonants and vowels are calculated separately. The following picture explains this method using the name ``CHRISTOPHER RORY PAGE".
So you can see that to find the consonant value, the values of individual consonants are added and to find the vowel value the values of individual vowels are added. A mad Numerologist suggests people many strange lucky names. He follows the rules stated below while giving lucky names.
- The name has a predefined length N.
- The vowel value and consonant value of the name must be kept minimum.
- To make the pronunciation of the name possible vowels and consonants are placed in alternate positions. Actually vowels are put in odd positions and consonants are put in even positions. The leftmost letter of a name has position 1; the position right to it is position 2 and so on.
- No consonants can be used in a name more than five times and no vowels can be used in a name more than twenty-one times
- Following the rules and limitations above the name must be kept lexicographically smallest. Please note that the numerologists first priority is to keep the vowel and consonant value minimum and then to make the name lexicographically smallest.
Input
First line of the input file contains an integer N ( 0 < N250) that indicates how many sets of inputs are there. Each of the next N lines contains a single set of input. The description of each set is given below: Each line contains an integer n ( 0 < n < 211) that indicates the predefined length of the name.
Output
For each set of input produce one line of output. This line contains the serial of output followed by the name that the numerologist would suggest following the rules above. All letters in the output should be uppercase English letters.
Sample Input
3 1 5 5
Sample Output
Case 1: A Case 2: AJAJA Case 3: AJAJA
Miguel Revilla 2004-12-02
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> int main() { int T, n, i, j, odn = 5, evn = 21, o_cnt, e_cnt, o_re, e_re, cnt = 0, t ; char numer[220], hash; char odd[] = "AUEOI", even[] = "JSBKTCLDMVNWFXGPYHQZR"; scanf("%d", &T); while(T--) { scanf("%d", &n); o_re = e_re = o_cnt = e_cnt = 0; memset(numer, 0, sizeof(numer)); for(i=0; i<n; ++i) {// 提取满足要求的vowels 和 consonants if((i+1)%2) { numer[i] = odd[o_re]; if(++o_cnt >= 21) o_cnt = 0, o_re++; } else { numer[i] = even[e_re]; if(++e_cnt >= 5) e_cnt = 0, e_re++; } } numer[i] = '\0'; for(t=0; t<=1; ++t) {// 分别对奇偶的位置跟据字符ASCII的大小进行一次直接插入排序,达到按字典序排的目的 for(i=t+2; i<n; i+=2) { if(numer[i]-numer[i-2] < 0) { hash = numer[i]; for(j=i-2; j>=0; j-=2) { if(hash - numer[j] < 0) { numer[j+2] = numer[j]; } else { numer[j+2] = hash; break; } } numer[j+2] = hash; } } } printf("Case %d: %s\n", ++cnt, numer); } return 0; }
解题思路:
首先清楚题目的意思
首先要满足的条件是 sum(vowels) && sum(consonants)的值都要达到最小,那么就要分别从postion left -> postion right 的方向找需要满足的字符;
再者,要满足按字典顺序找,那么对于 vowels,如果存在不同的vowels,那么就要将其按字典序进行排序,对于consonants,刚开始我想到的是:像寻找vowels一样,在满足从left到right的前提下再从up到down的方向去找,这样做的时候我直接提交了一次,WA了。
后来发现必须要考虑到consonants在不同的postion的情况,所以用了直接插入排序法将consonants的顺序按字典序排序后输出(这时up到down的查找顺序还是起到AC的作用,这一步也必须要有)
AC了
更多内容请关注个人微信公众号 物役记 (微信号:materialchains)