Abbreviation Time Limit: 2 Seconds       Memory Limit: 65536 KB

When a Little White meets another Little White:

Little White A: (Surprised) !
Little White B: ?
Little White A: You Little White know "SHDC"? So unbelievable!
Little White B: You are little white! Little white is you! What is "SHDC" you are talking about?
Little White A: Wait... I mean "Super Hard-disc Drive Cooler".
Little White B: I mean "Spade Heart Diamond Club"... Duck talks with chicken -_-//
Little White A: Duck... chicken... faint!

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

------quote from qmd of Spade6 in CC98 forum.

Sometimes, we write the abbreviation of a name. For example IBM is the abbreviation for International Business Machines. A name usually consists of one or more words. A word begins with a capital letter ('A' - 'Z') and followed by zero or more lower-case letters ('a' - 'z'). The abbreviation for a name is the word that consists of all the first letters of the words.

Now, you are given two names and asked to decide whether their abbreviations are the same.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T which is the number of test cases. And it will be followed by Tconsecutive test cases.

There are four lines for each case.
The first line contains an integer N (1 <= N <= 5), indicating the number of words in the first name.
The second line shows the first name.
The third line contains an integer M (1 <= M <= 5), indicating the number of words in the second name.
The fourth line shows the second name.
Each name consists of several words separated by space. Length for every word is less than 10. The first letter for each word is always capital and the rest ones are lower-case.

Output

Results should be directed to standard output. The output of each test case should be a single line. If two names' abbreviations are the same, output "SAME", otherwise output "DIFFERENT".

Sample Input

3
4
Super Harddisc Drive Cooler
4
Spade Heart Diamond Club
3
Shen Guang Hao
3
Shuai Ge Hao
3
Cai Piao Ge
4
C P C S

Sample Output

SAME
SAME
DIFFERENT

我超耿直(笨)的做法:
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int T;
 8     scanf("%d",&T);
 9 
10     while(T--){
11           bool flag=0;
12           int N,M;
13           char a[10];
14           char b[10];
15           scanf("%d\n",&N);
16           gets(a);
17           scanf("%d\n",&M);
18           gets(b);
19           if(N!=M){
20             printf("DIFFERENT\n");
21             continue;
22           }
23 
24           if(a[0]==b[0]){
25             int indexa=1;
26             int indexb=1;
27             while(1){
28                 while((a[indexa]>90||a[indexa]<65)&&a[indexa]!='\0'){
29                     indexa++;
30                 }
31                 while((b[indexb]>90||b[indexb]<65)&&b[indexb]!='\0'){
32                     indexb++;
33                 }
34                 //printf("a%d: %d  b%d:  %d\n",indexa,a[indexa],indexb,b[indexb]);
35                 if(b[indexb]=='\0'||a[indexa]=='\0'){
36                     flag=1;
37                     break;
38                 }
39                 if(a[indexa++]!=b[indexb++]){
40                     break;
41                 }
42             }
43           }
44           if(flag) printf("SAME\n");
45           else printf("DIFFERENT\n");
46     }
47     return 0;
48 }

1.一开始那个=='\0'写成了=='\n',睡醒了再做题!

2.一定要注意输入NM时的\n,不然会影响字符数组a,b的输入

顺便复习一下A-Z,a-z的ASCII:

A-Z:65-90 a-z:97-122

试了一下用空格识别的方法,也是可以的

 1    while(1){
 2                 while(a[indexa]!=' '&&a[indexa]!='\0'){
 3                     indexa++;
 4                 }
 5                 while(b[indexb]!=' '&&b[indexb]!='\0'){
 6                     indexb++;
 7                 }
 8                 //printf("a%d: %d  b%d:  %d\n",indexa,a[indexa],indexb,b[indexb]);
 9                 if(b[indexb]=='\0'||a[indexa]=='\0'){
10                     flag=1;
11                     break;
12                 }
13                 if(a[++indexa]!=b[++indexb]){
14                     break;
15                 }
16             }

参考别人的答案之后,想到既然只需要对比两个串的大写,那么为什么不只储存大写呢!

下面这个代码,测试用例都是对的,而且我也打印过单步的值都没错啊!但是不知道为什么就是不可以AC!!!

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int T;
 8     scanf("%d",&T);
 9 
10     while(T--)
11     {
12         bool flag=1;
13         int N,M;
14         char a[10];
15         char b[10];
16         scanf("%d\n",&N);
17 
18         for(int i=0; i<N; i++)
19         {
20             char t[12];
21             scanf("%s",t);
22             a[i]=t[0];
23         }
24 
25         scanf("%d\n",&M);
26 
27         if(N!=M)
28         {
29             printf("DIFFERENT\n");
30             continue;
31         }
32 
33         for(int j=0; j<N; j++)
34         {
35             char t[20];
36             scanf("%s",t);
37             b[j]=t[0];
38 
39         }
40         for(int k=0;k<N;k++){
41             
42             if(a[k]!=b[k]){
43                 flag=0;
44                 break;
45             }
46         }
47 
48         if(flag)
49             printf("SAME\n");
50         else
51             printf("DIFFERENT\n");
52     }
53     return 0;
54 }

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄