ACM书中的题目 Z-26

Z-26

Description
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!

——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 T consecutive 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

题目简述:
判断两组单词首字母是否相同
解题思路:
用两组字符串来储存输入内容,再用两组字符串来分别储存首字母,最后判断两组首字母是否相同
解题细节:
用四组字符串就可解决,但也可以用两组字符串数组,不过要分别判断首字母
代码:

#include<iostream>
using namespace std;
int main()
{
    int t,sa,sb;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        string a,b,c,d;
        cin>>sa;
        for(int q=0;q<sa;q++)
        {
            cin>>a;
            c+=a[0];
        }
        cin>>sb;
        for(int j=0;j<sb;j++)
        {
            cin>>b;
            d+=b[0];
        }
        if(c==d)
        cout<<"SAME"<<endl;
        else
        cout<<"DIFFERENT"<<endl;
  }
}

体会:
第二种方法尝试过但失败了
代码如下

#include<iostream>
using namespace std;
int main()
{
    int t,sa,sb,z,tr;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        string a[5],b[5];
        cin>>sa;
        for(int q=0;q<sa;q++)
        {
            cin>>a[q];
        }
        cin>>sb;
        for(int q=0;q<sb;q++)
        {
            cin>>b[q];
        }
        if(sa>=sb)
        tr=sa;
        else
        tr=sb;
        for(int q=0;q<tr;q++)
        {
            if(a[q][1]==b[q][1])
            z=1;
            else
            z=0;
        }
        if(z==1)
        cout<<"SAME"<<endl;
        else
        cout<<"DIFFERENT"<<endl;
  }
}

以后再看吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值