《ACM程序设计》书中题目 Z - 26 名字缩写
2017-03-19 21:42
302 查看
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
Sample Output
这道题的基本题意为输入n组数据,每组数据两个名字,比较这两个名字每个单词首字母是否都相同。
基本思路为将每个单词的首字母储存在另一个字符数组中,然后转换为string型,比较他们两个是否相等。
源代码如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m1,m2,i,j;
char a[11];
string x,y;
cin>>n;
for(i=0;i<n;++i)
{ char b[6]={'\0'},c[6]={'\0'};
cin>>m1;
for(j=0;j<m1;++j)
{ cin>>a;
b[j]=a[0];
}
cin>>m2;
for(j=0;j<m2;++j)
{ cin>>a;
c[j]=a[0];
}
x=b;
y=c;
if(x==y)cout<<"SAME"<<endl;
else cout<<"DIFFERENT"<<endl;
}
}
需要注意的地方在于先将首字母储存在一个字符数组中,然后再赋给string型变量,如果直接储存在string型数组中比较的时候有点难比较。
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
这道题的基本题意为输入n组数据,每组数据两个名字,比较这两个名字每个单词首字母是否都相同。
基本思路为将每个单词的首字母储存在另一个字符数组中,然后转换为string型,比较他们两个是否相等。
源代码如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m1,m2,i,j;
char a[11];
string x,y;
cin>>n;
for(i=0;i<n;++i)
{ char b[6]={'\0'},c[6]={'\0'};
cin>>m1;
for(j=0;j<m1;++j)
{ cin>>a;
b[j]=a[0];
}
cin>>m2;
for(j=0;j<m2;++j)
{ cin>>a;
c[j]=a[0];
}
x=b;
y=c;
if(x==y)cout<<"SAME"<<endl;
else cout<<"DIFFERENT"<<endl;
}
}
需要注意的地方在于先将首字母储存在一个字符数组中,然后再赋给string型变量,如果直接储存在string型数组中比较的时候有点难比较。
相关文章推荐
- ACM程序设计题目 Problem Z-26
- 《ACM程序设计》书中题目Z-26
- 《ACM程序设计》书中题目J 第十题 转化字母缩写
- 《ACM程序设计》书中题目 N-14 颠倒数字相加
- 1601: 名字缩写
- 《ACM程序设计》书中题目 L
- 《ACM程序设计》书中题目T 第二十题 找底数
- acm书中题目 z-26
- 《ACM程序设计》书中题目T(谁的几次方最接近它O.O。。。)
- 《ACM程序设计》书中题目 X - 24 打电话
- 《ACM程序设计》-Problem-Z-比较英文缩写是否相同
- 《ACM程序设计》书中题目 Z
- 《ACM程序设计》书中题目 Y
- nyoj 题目1285 缩写恢复
- leetcode做题总结,题目3Sum,4Sum,3Sum Closest---- 2012/01/16-17-26
- [BZOJ]4491: 我也不知道题目名字是什么 线段树(差分)
- 《ACM程序设计》书中题目--problem j
- 《ACM程序设计》书中题目 U
- 《ACM程序设计》书中题目L(写字顺序)
- 《ACM程序设计》书中题目M