BNU 3692 I18n 模拟

题意:

在一篇文章中,单词可以缩写.例如单词Internationalization可以缩写为I18n,缩写的两端是原单词的首尾字母,中间的数字是被省略的字母的个数.

现在给你一篇缩写的文章,输出展开后的文章.

一个被缩写的单词展开有条件限制:

  • 之前出现过的单词中有且只有一个符合这种缩写形式

展开缩写的时候还有一个规则:

  • 如果缩写形式首尾字母都是小写,那么展开后的单词字母全部小写
  • 如果缩写形式首尾字母都是大写,那么展开后的单词字母全部大写
  • 如果缩写形式首字母大写,尾字母小写,那么展开后的单词首字母大写,其余字母小写
  • 只有以上三种情况

比如:

Sample

s4e --> sample

S4e --> Sample

S4E --> SAMPLE

分析:

我开了一个 std::set<string> S[L][R][x]用来保存出现过的单词中 符合首字母为L尾字母为R中间省略了x个字母的单词的集合.

在读入的时候:

  • 如果遇到特殊符号直接输出不用处理.
  • 如果遇到单词,原样输出,然后把它统一处理成小写的形式插入到对应的集合.
  • 如果遇到缩写形式:
    • 如果对应单词集合的size为1,那么按照题中要求的规则输出.
    • 否则该单词不能被展开,原样输出.
 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("
");
80     }
81 
82     return 0;
83 }
代码君
原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4856399.html