Description
在英文文献中,尤其是专业文献中,经常有很多的缩略词,如CPU代表Central Processing Unit等。为了方便学习,Qili决定从一批英文论文中提取出所有的缩略词以及它们的全称。
经过初步判断,这些文章的缩略词都是以全部大写字母的形式出现,而且每个缩略词之后会有一个空格,之后是它的全称。全称使用“()”括起来,左括号跟它后面的单词之间没有空格,右括号跟它前面的单词之间没有空格,全称的每个单词的首字母与缩略词的顺序是对应的。全称的单词之间可能有连字符“-”连接。
你来帮Qili编写这个程序,将所有的缩略词和全称提取出来。
Input
一篇英文文章,每个缩略词第一次出现时,其后都跟有它的英文全称;同样的缩略词再出现时,将不再出现全称。每个缩略词和全称都不会太长。缩略词总数小于100。
Output
如果有缩略词,第一行输出“Abbreviation ==> Full Name”。之后每个缩略词和它的全称占一行,包括缩略词的序号(从1开始)、缩略词、分隔符(==>)和全称。所有输出以分隔符(==>)分为两部分,右侧的全称左对齐,左侧的缩略词右对齐,但序号和第一行的“Abbreviation”是左对齐的。每个缩略词只输出一遍。
如果没有缩略词,则输出:There is no abbreviations in this text.
Sample Input
Sample Output
HINT
以下贴一下我的代码:
1 #include <stdio.h> 2 #include <ctype.h> 3 #include <string.h> 4 char str[1000000]; 5 void printste(char *s,char *e){ 6 while(s<=e)putchar(*s++); 7 } 8 int is_upper(char *s,char *e){ 9 while(s<=e)if(!isupper(*s++))return 0; 10 return 1; 11 } 12 int match_word(char *s,char *e,char *as,char *ae){ 13 if(s>e||as>ae||*(e)==' ')return 0; 14 while(s<=e&&as<=ae){ 15 if(*as++!=toupper(*s++))return 0; 16 while(*s!=' '&&*s!='-')s++; 17 s++; 18 } 19 if(s<=e)return 0; 20 return 1; 21 } 22 void search(char * p){ 23 int cnt=0; 24 char * p0=p,*p1; 25 while(p0!=NULL){ 26 int ok=1; 27 char *p3;//p0代表(,p1代表),p3代表缩略词首字母 28 if((p0=strchr(p0,'('))==NULL)break; 29 if((p1=strchr(p0+1,')'))==NULL)break; 30 p3=p0-2; 31 if(p3-p>=0&&*(p3+1)==' ')while(p3-p>=0&&isupper(*(p3)))p3--;//防止下标越界 32 else ok=0; 33 if(islower(*(p3))) ok=0; 34 if(ok&&match_word(p0+1,p1-1,++p3,p0-2)){ 35 if(!cnt)printf("Abbreviation ==> Full Name\n"); 36 printf("%d:",++cnt); 37 *(p0-1)='\0'; 38 if((cnt)/10==0)printf("%10s",p3); 39 else if((cnt)/100==0)printf("%9s",p3); 40 *(p0-1)=' '; 41 printf(" ==> "); 42 printste(p0+1,p1-1); 43 printf("\n"); 44 } 45 p0=p1; 46 } 47 if(cnt==0) printf("There is no abbreviations in this text.\n"); 48 } 49 int main(){ 50 int ch; 51 int k=0; 52 while((ch=getchar())!=EOF) 53 str[k++]=(ch=='\n')?' ':ch; 54 str[k]=' ';str[k++]=' ';str[k++]=0;//防止字符串末尾下标越界 55 search(str); 56 return 0; 57 }