2.6 Abbreviation( 缩 写)

                                    2.6 Abbreviation

2.6.1 时空限制

        Time limit: 1 Seconds Memory limit: 32768K 

2.6.2 题目内容

        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 forInternational 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 

2.6.3 题目来源

         Zhejiang University Local Contest 2008(Author: HANG, Hang) 

2.6.4 汉语翻译

    1.题目

                                                       缩 写 

       当 Little White A 遇到 Little White B:
       Little White A:(吃惊)!
       Little White B:?
       Little White A:你知道“SHDC”?难以置信!
       Little White B:你是 Little White! Little White 是你!你指的“SHDC”是什么?
       Little White A:等等,我是指“Super Hard-disc Drive Cooler”。
       Little White B:我以为是“Spade Heart Diamond Club”。真是鸭同鸡讲话-_-// 
       Little White A:鸭,鸡,晕!

                                                                                               ——摘自 qmd of Spade6,CC98 论坛。

       我们经常写缩写。如 IBM 是 International Business Machines 的缩写。一个名字通常包含多个单词。一个单词以一个大写字母打头(“A”~“Z”),后面不跟或跟多个小写字母(“a”~“z”)。缩写是由每个单词的首字母组成。
       现在,给出两个名字,要求你说出这两个名字的缩写是否相同。

      2.输入描述

        标准输入将包含多个测试案例。输入的第一行是一个整数 T,代表测试案例的个数。然后,是 T 组测试案例。
        一组测试案例有四行。
        第一行是一个整数 N(1≤N≤5),表示第一个名字中单词的个数。
        第二行显示了第一个名字。
        第三行是一个整数 M(1≤M≤ 5),表示第二个名字中单词的个数。
        第四行显示了第二个名字。
       每个名字由多个单词组成,单词间用一个空格分开。每个单词的长度少于 10 个。每个单词的第一个字母是大写字母,其余字母是小写字母。

      3.输出描述

        结果应为标准输出。每个测试案例输出一行。如果两个名字的缩写相同,则输出“SAME”,否则,输出“DIFFERENT”。

      4.输入样例

             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

       5.输出样例

             SAME 
             SAME 
             DIFFERENT 

#include <iostream>
#include <string>  
using namespace std; 
int main(int argc, char* argv[]) 
{ 
    string s[5],t[5];
	int d,m,n,i,j;
	cin>>d;
	for(i=0;i<d;i++)
	{
		cin>>m;
		for(j=0;j<m;j++)
		{
			cin>>s[j];
		}
		cin>>n;
		for(j=0;j<n;j++)
		{
			cin>>t[j];
		}
		if(m!=n)
		{
			cout<<"DIFFERENT"<<endl;
			continue;
		}
		else
		{
			int num=0;
			for(j=0;j<n;j++)
			{
				if(s[j][0]==t[j][0])
				{
					num++;
				}
			}
			if(num==n)
			{
				cout<<"SAME"<<endl;
			}
			else
			{
				cout<<"DIFFERENT"<<endl;
			}
		}
	}
    return 0; 
} 

2.6.5 解题思路

      本题是比较两个缩写词是否相同,而缩写词又是从一个包含多个单词的名字中合成的。每次读入一个单词,然后取出它的第一个字母,连接在字符串上,就组成了一个缩写词。
         本题的难点在单词的读取控制上,对于输入数据的控制,是 ACM 竞赛中考查的一个重要方面。
         另外,大家可以试试,使用 printf 输出比使用 cout 输出快很多。

 2.6.6  参考答案

#include <iostream>
#include <string>  
using namespace std; 
int main(int argc, char* argv[]) 
{  
    string s,ssa,ssb; 
    int t,n,m; 
    cin>>t; 
    for(int i=0;i<t;i++) 
	{ 
		cin>>n; 
		for(int j=0;j<n;j++) 
		{ 
			cin>>s; 
			ssa=ssa + s[0]; 
		} 
		cin>>m; 
		for(int k=0;k<m;k++) 
		{ 
			cin>>s; 
			ssb=ssb + s[0]; 
		} 
		if(ssa.compare(ssb)==0)//相等返回 0,大于返回 1,小于返回-1 
			//cout<<"SAME"<<endl; 
			printf("SAME\n"); 
		else 
			//cout<<"DIFFERENT"<<endl; 
			printf("DIFFERENT\n"); 
		ssa=""; 
		ssb=""; 
	}
    return 0; 
} 
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值