24h購物| | PChome| 登入
2013-08-23 09:20:01| 人氣945| 回應0 | 上一篇 | 下一篇

[UVA] 373 - Romulan Spelling

推薦 0 收藏 0 轉貼0 訂閱站台


 Romulan Spelling 

The Romulans use a language that can be approximated with the English alphabet and normal punctuation marks. Their spelling rules are even stranger than English, however. In particular, they have a rule that states:

G before P except after E or when pronounced X as in npgukbor or wpguk.

Operationally, you can detect the X pronounciation by the string PGUK appearing in the word. Also, the Romulan rules for capitalization are different from ours, so capital letters can appear anywhere in a word.

Input and Output

Given a file containing lines of Romulan text, you are to output the text with spelling corrected according to the G before P except after E rule given above. No input line will contain more than 70 characters including spaces.

For example, the input file corresponding to the romulan translation of the quote:

"I received the wierd piece of pie from my neighbor sam who in turn recieved the weird peice of pei from his nieghbor harry," might well be ...

Sample Input

I rpEpgvpd tKp wgprd tgpEp of tgp from my npguKbor sam wKo gn turn
rpEgpvpd tKp wpgrd tpgEp of tpg from Kgs ngpuKbor Karry,

Sample Output

I rpEpgvpd tKp wgprd tgpEp of tgp from my npguKbor sam wKo gn turn
rpEpgvpd tKp wgprd tgpEp of tgp from Kgs npguKbor Karry,

題目描述:

「G before P except after E or when pronounced X as in npgukbor or wpguk. 」
現在我們要從一般的英文單字中,轉換成 羅幕倫人 用的發音語法。
這是個艱難的任務,正如你所看到的那令人敬畏的題目說明。
出題者大概是程式寫多了,說話就跟 code 一樣。
具體翻譯如下:

'G' 必須在 'P' 前面,因此先把所有可能轉換對於相鄰的 pg -> gp。
除非 'E' 在前面,則保留原本的順序,即 epg -X-> egp。
除非字根 X 出現,也保留原本順序,其中 X = pguk。

照個順序做才會正確,否則都是徒然。
有很多模稜兩可的情況,例如 eggpuk 應該被念成 epgguk。



#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char tolower(char c) {
if(c >= 'A' && c <= 'Z')
c |= 32;
return c;
}
void romulan(char s[]) {
int update = 0, i;
static char ts[1024];
strcpy(ts, s);
do {
do {
update = 0;
for(i = 0; s[i]; i++) {
if(tolower(s[i]) == 'p' && tolower(s[i+1]) == 'g') {
swap(s[i], s[i+1]);
update = 1;
}
}
} while(update);
do {
update = 0;
for(i = 0; s[i]; i++) {
if(tolower(s[i]) == 'g' && tolower(s[i+1]) == 'p') {
if(i-1 >= 0 && tolower(s[i-1]) == 'e')
swap(s[i], s[i+1]), update = 1;
else if(tolower(s[i+2]) == 'u' && tolower(s[i+3]) == 'k')
swap(s[i], s[i+1]), update = 1;
}
}
} while(update);
update = strcmp(ts, s);
strcpy(ts, s);
} while(update);
}
int main() {
char s[1024], word[1024];
while(gets(s)) {
int i, j;
for(i = 0; s[i]; i++) {
if((s[i]|32) < 'a' || (s[i]|32) > 'z') {
putchar(s[i]);
} else {
for(j = 0; (s[i]|32) >= 'a' && (s[i]|32) <= 'z'; i++, j++)
word[j] = s[i];
word[j] = '\0';
romulan(word);
printf("%s", word);
i--;
}
}
puts("");
}
return 0;
}
 

台長: Morris
人氣(945) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][字串] 426 - Fifth Bank of Swamp County
此分類上一篇:[UVA][math] 1406 - A Sequence of Numbers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文