1. 程式人生 > >【列舉】Consonant Fencity @upcexam5110

【列舉】Consonant Fencity @upcexam5110

時間限制: 3 Sec 記憶體限制: 512 MB
題目描述
There are two kinds of sounds in spoken languages: vowels and consonants. Vowel is a sound, produced with an open vocal tract; and consonant is pronounced in such a way that the breath is at least partly obstructed. For example, letters a and o are used to express vowel sounds, while letters b and p are the consonants (e.g. bad, pot).
Some letters can be used to express both vowel and consonant sounds: for example, y may be used as a vowel (e.g. silly) or as a consonant (e.g. yellow). The letter w, usually used as a consonant (e.g. wet) could produce a vowel after another vowel (e.g. growth) in English, and in some languages (e.g. Welsh) it could be even the only vowel in a word.
In this task, we consider y and w as vowels, so there are seven vowels in English alphabet: a, e, i, o, u, w and y, all other letters are consonants.
Let’s define the consonant fencity of a string as the number of pairs of consecutive letters in the string which both are consonants and have different cases (lowercase letter followed by uppercase or vice versa). For example, the consonant fencity of a string CoNsoNaNts is 2, the consonant fencity of a string dEsTrUcTiOn is 3 and the consonant fencity of string StRenGtH is 5.
You will be given a string consisting of lowercase English letters. Your task is to change the case of some letters in such a way that all equal letters will be of the same case (that means, no letter can occur in resulting string as both lowercase and uppercase), and the consonant fencity of resulting string is maximal.
輸入
The only line of the input contains non-empty original string consisting of no more than 106 lowercase English letters.
輸出
Output the only line: the input string changed to have maximum consonant fencity.
樣例輸入
consonants
樣例輸出
coNsoNaNts

把26個字母分成19個子音字母和7個母音字母,讓你通過改變子音字母的大小寫狀態,使得字串中連續的兩個大小寫狀態不同的子音字母組成的字母對數量最多,輸出該狀態下的字串。
掃一遍字串,統計每種子音字母對的數量,總共19*19種。
列舉19個子音字母的大小寫狀態(二進位制狀壓),內迴圈列舉每兩個子音字母的前後關係,維護一個最大值
複雜度 o(219192)

#define IN_LB() freopen("F:\\in.txt","r",stdin)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h> using namespace std; typedef long long ll; int comb[20][20]; int mapp[] = {0,1,2,3,0,4,5,6,0,7,8,9,10,11,0,12,13,14,15,16,0,17,0,18,0,19}; char s[1000005]; void out(int sta) { for(int i=0; s[i]; i++) { if(sta&(1<<(mapp[s[i]-'a']-1))) { printf("%c"
,s[i]-'a'+'A'); } else printf("%c",s[i]); } printf("\n"); } int main() { // IN_LB(); scanf("%s",s); for(int i=1; s[i]; i++) { comb[mapp[s[i-1]-'a']][mapp[s[i]-'a']]++; } int ans = 0,maxn = 0,sum = 1<<19; for(int i=0; i<sum; i++) { int cnt = 0; for(int j=1; j<=19; j++) { for(int k=1; k<=19; k++) { if( (i&(1<<(j-1))&&!(i&(1<<(k-1)))) || ((!(i&(1<<(j-1))))&&(i&(1<<(k-1)))) ) { cnt += comb[j][k]; } } } if(cnt>maxn) { maxn = cnt; ans = i; } } out(ans); return 0; }