Gym 101190A------- Abbreviation(字符串 模拟)

http://codeforces.com/gym/101190/attachments

 

Problem A.Abbreviation

Input file: abbreviation.in

Output file: abbreviation.out

 An abbreviation (from Latin brevis, meaning short) is a shortened form of a word or phrase. In this problem you must write an automated tool that replaces a sequence of capitalized words with the corresponding abbreviation that consists of the first upper case letters only, followed by a full definition in parenthesis. See sample input and output.

 Let us make some formal definitions. A word in a text is a maximally long sequence of lower and upper case English letters. A capitalized word is a word that consists of an upper case letter followed by one or more lower case letters. For example, “Ab”, “Abc”, “Abcd”, and “Abcde“ are all capitalized words, while “ab”, “A”, “AB“, “ABc“ and “AbC“ are not.

 An abbreviatable sequence of words is a sequence of two or more capitalized words that are separated by exactly one space, no line breaks or punctuation are allowed inside it.

 An abbreviation of an abbreviatable sequence of words is a sequence of the first (upper case) letters of each word, followed by a single space, an opening parenthesis, the original abbreviatable sequence, and a closing parenthesis.

Input

 The input file consists of up to 1 000 lines of text with up to 120 characters on each line. Each line consists of spaces, upper and lower case letters, commas or dots. There are no leading or trailing spaces on lines and there are no empty lines. There is at least one line in the input file.

Output

 Write to the output file the original text with every abbreviatable sequence of words replaced with the corresponding abbreviation.

 

Examples

 

abbreviation.in

This is ACM North Eastern European Regional Contest,

sponsored by International Business Machines.

The. Best. Contest. Ever.

A Great Opportunity for all contestants.

abbreviation.out

This is ACM NEERC (North Eastern European Regional Contest),

sponsored by IBM (International Business Machines).

The. Best. Contest. Ever.

A GO (Great Opportunity) for all contestants.

 

abbreviation.in

ab Ab A Abc AB Abcd ABc Abcde AbC

abbreviation.out

ab Ab A Abc AB Abcd ABc Abcde AbC

 

abbreviation.in

Oh  No  Extra Spaces.And,Punctuation Ruin Everything

abbreviation.out

Oh  No  ES (Extra Spaces).And,PRE (Punctuation Ruin Everything)

 

 我的思路是把单词和单词间隔分别提出来,存到数组b和c,后期好处理一点,细节很多,需要耐心

  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 int judge_xiao(char ch)
  5 {
  6     if(ch>='a'&&ch<='z') return 1;
  7     else return 0;
  8 }
  9 
 10 int judge_da(char ch)
 11 {
 12     if(ch>='A'&&ch<='Z') return 1;
 13     else return 0;
 14 }
 15 
 16 int len, i, curb, curc, j, k;
 17 int book[10005], book1[10005];
 18 char a[10005], b[10005][10005], c[10005][10005];
 19 
 20 int main()
 21 {
 22     freopen("abbreviation.in", "r", stdin);
 23     freopen("abbreviation.out", "w", stdout);
 24     while(gets(a))
 25     {
 26         memset(book, 0, sizeof(book));
 27         memset(book1, 0, sizeof(book1));
 28         len = strlen(a);
 29         curb = 0;
 30         curc = 0;
 31 
 32         for(i=0; i<len; i++)
 33         {
 34             j = 0;
 35             while((judge_xiao(a[i])||judge_da(a[i]))&&i<len)
 36             {
 37                 b[curb][j++] = a[i];
 38                 i++;
 39             }
 40             b[curb][j] = '\0';
 41             curb++;
 42             if(i<len)
 43             {
 44                 j = 0;
 45                 while((judge_xiao(a[i])==0&&judge_da(a[i])==0)&&i<len)
 46                 {
 47                     c[curc][j++] = a[i];
 48                     i++;
 49                 }
 50                 c[curc][j] = '\0';
 51                 curc++;
 52                 i--;
 53             }
 54 
 55         }
 56 
 57         for(i=0; i<curb; i++)
 58         {
 59             len = strlen(b[i]);
 60             if(judge_da(b[i][0])==0||len==1) continue;
 61             else
 62             {
 63                 j = 1;
 64                 while(judge_xiao(b[i][j])==1&&j<len)
 65                 {
 66                     j++;
 67                 }
 68                 if(j>=len) book[i] = 1;
 69             }
 70         }
 71 
 72         for(i=0; i<curc; i++)
 73         {
 74             if(strcmp(c[i], " ")==0) book1[i] = 1;
 75         }
 76 
 77 
 78         book1[curc] = 1;
 79         int num;
 80         for(i=0; i<curb; i++)
 81         {
 82             k = i;
 83             num = 0;
 84             while(book[k]==1&&k<curb)
 85             {
 86                 num++;
 87                 if(book1[k]==0) break;
 88                 k++;
 89             }
 90             if(book[k]==0||k>=curb) k--;
 91             if(num>1)
 92             {
 93                 for(j=i; j<=k; j++)
 94                 {
 95                     printf("%c", b[j][0]);
 96                 }
 97                 printf(" ");
 98                 printf("(");
 99                 for(j=i; j<=k; j++)
100                 {
101                     if(j==k) printf("%s)", b[j]);
102                     else printf("%s", b[j]);
103                     if(j<curc) printf("%s", c[j]);
104                 }
105                 i = k;
106             }
107             else
108             {
109                 printf("%s", b[i]);
110                 if(i<curc) printf("%s", c[i]);
111             }
112         }
113         printf("\n");
114     }
115     return 0;
116 }

 

转载于:https://www.cnblogs.com/0xiaoyu/p/11398500.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值