题意:
在一篇文章中,单词可以缩写.例如单词Internationalization可以缩写为I18n,缩写的两端是原单词的首尾字母,中间的数字是被省略的字母的个数.
现在给你一篇缩写的文章,输出展开后的文章.
一个被缩写的单词展开有条件限制:
展开缩写的时候还有一个规则:
比如:
Sample
s4e --> sample
S4e --> Sample
S4E --> SAMPLE
分析:
我开了一个 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 }
原文:http://www.cnblogs.com/AOQNRMGYXLMV/p/4856399.html