《ACM程序设计》书中题目―Z

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

1、题意:让你输入一串字符,然后判断他们的首字母缩写是否相同,相同输出SAME,不同输出DIFFERENT。
1、思路:使用string然后提取数组的第一个字符就可以得到首字母了,然后再把它们存到一个字符数组中,然后两个字符串提取出来的首字母比较即可。
3、代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n1,n2,i,j,k,t;
char a[100],b[100];
string c,d;
cin>>t;
while(t--)
	{
  cin>>n1;
	for(i=0;i<n1;i++)
		{
		cin>>c;
		a[i]=c[0];
		}
	cin>>n2;
	for(i=0;i<n2;i++)
		{
		cin>>d;
		b[i]=d[0];
		}
	if(n1!=n2)
		cout<<"DIFFERENT"<<endl;
	else
		{
		for(j=0,i=0;i<n1;i++)
			{
			if(a[i]==b[i])
			j++;
			}
		if(j==n1)
			cout<<"SAME"<<endl;
		else
			cout<<"DIFFERENT"<<endl;
		}
	}
return 0;
}
4、总结:只使用了string,一道简单的题目,只需要注意细节即可。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值