題意:ios
在一篇文章中,單詞能夠縮寫.例如單詞Internationalization能夠縮寫爲I18n,縮寫的兩端是原單詞的首尾字母,中間的數字是被省略的字母的個數.ide
如今給你一篇縮寫的文章,輸出展開後的文章.spa
一個被縮寫的單詞展開有條件限制:code
展開縮寫的時候還有一個規則:blog
好比:ci
Sampleget
s4e --> samplestring
S4e --> Sampleit
S4E --> SAMPLEio
分析:
我開了一個 std::set<string> S[L][R][x]用來保存出現過的單詞中 符合首字母爲L尾字母爲R中間省略了x個字母的單詞的集合.
在讀入的時候:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <string> 6 #include <set> 7 using namespace std; 8 9 bool isAlpha(char c) { 10 if('a' <= c && c <= 'z') return true; 11 if('A' <= c && c <= 'Z') return true; 12 return false; 13 } 14 15 bool isNum(char c) { return '0' <= c && c <= '9'; } 16 17 bool ok(char c) { return isAlpha(c) || isNum(c); } 18 19 int id(char c) { 20 if('a' <= c && c <= 'z') return c - 'a'; 21 return c - 'A'; 22 } 23 24 bool isBig(char c) { return 'A' <= c && c <= 'Z'; } 25 26 char Toup(char c) { 27 if('A' <= c && c <= 'Z') return c; 28 return 'A' + c - 'a'; 29 } 30 31 char Tolow(char c) { 32 if('a' <= c && c <= 'z') return c; 33 return 'a' + c - 'A'; 34 } 35 36 set<string> S[55][55][55]; 37 38 string line; 39 40 int main() 41 { 42 //freopen("in.txt", "r", stdin); 43 44 while(getline(cin, line)) { 45 int l = line.length(); 46 int s, t; 47 for(s = 0; s < l; s++) { 48 if(isAlpha(line[s])) 49 { 50 for(t = s; t < l && ok(line[t]); t++); t--; 51 string sub = line.substr(s, t - s + 1); 52 int len = sub.length(); 53 int lft = id(sub[0]), rgh = id(sub[len-1]); 54 if(len > 1 && isNum(sub[1])) { 55 int x = 0; 56 for(int i = 1; i < len && isNum(sub[i]); i++) 57 x = x * 10 + sub[i] - '0'; 58 59 if(x >= 2 && (int)S[lft][rgh][x].size() == 1) { 60 string ans = *(S[lft][rgh][x].begin()); 61 int _len = ans.length(); 62 if(isBig(sub[0])) ans[0] = Toup(ans[0]); 63 if(isBig(sub[len-1])) for(int i = 1; i < _len; i++) ans[i] = Toup(ans[i]); 64 cout << ans; 65 } 66 else cout << sub; 67 } 68 else { 69 cout << sub; 70 if(len >= 4) { 71 for(int i = 0; i < len; i++) sub[i] = Tolow(sub[i]); 72 S[lft][rgh][len-2].insert(sub); 73 } 74 } 75 s = t; 76 } 77 else cout << line[s]; 78 } 79 printf("\n"); 80 } 81 82 return 0; 83 }