A language is a set of strings. And the concatenation of two languages is the set of all strings that are formed by concatenating the strings of the second language at the end of the strings of the first language. For example, if we have two language A and B such that:
A = {cat, dog, mouse}
B = {rat, bat}
The concatenation of A and B would be:
C = {catrat, catbat, dograt, dogbat, mouserat, mousebat}
Given two languages your task is only to count the number of strings in the concatenation of the two languages.
Input
There can be multiple test cases. The first line of the input file contains the number of test cases, T(1 ≤ T ≤ 25). Then T test cases follow. The first line of each test case contains two integers, M and N (M, N < 1500), the number of strings in each of the languages. Then the next M lines contain the strings of the first language. The N following lines give you the strings of the second language. You can assume that the strings are formed by lower case letters (‘a’ to ‘z’) only, that they are less than 10 characters long and that each string is presented in one line without any leading or trailing spaces. The strings in the input languages may not be sorted and there will be no duplicate string.
Output
For each of the test cases you need to print one line of output. The output for each test case starts with the serial number of the test case, followed by the number of strings in the concatenation of the second language after the first language.
Sample Input
2
3 2
cat
dog
mouse
rat
bat
1 1
abc
cab
Sample Output
Case 1: 6
Case 2: 1
这道题目实在是表述不清,虽说了字符串由“a to z”构成、不含空格,但是有要把空行当作字符串,于是:a+空行 和 空行+a 属于重复字符串。
因此,可用set容器自动排除重复元素,用 getline(cin,s) 读取字符串(使用getline时,要注意前面是否使用了 scanf 、cin,若有,需要getchar() 读取未被读取的 \n )。
#include<bits/stdc++.h>
using namespace std;
const int MAXSIZE=1505;
string s1[MAXSIZE];
string s2[MAXSIZE];
int main()
{
int n,eg=0,i,M,N,j;
string temp;
set <string> S;
cin>>n;
while(n--)
{
eg++;
cin>>M>>N;
getchar(); //读取cin遗留下的 \n
for(i=0;i<M;i++)
getline(cin,s1[i]);
for(i=0;i<N;i++)
getline(cin,s2[i]);
for(i=0;i<M;i++)
for(j=0;j<N;j++)
{
if(s1[i][0]==' '&&s2[j][0]!=' ')
{
S.insert(s2[j]);
}
else if(s1[i][0]!=' '&&s2[j][0]==' ')
{
S.insert(s1[i]);
}
else
{
temp=s1[i]+s2[j];
S.insert(temp);
}
}
cout<<"Case "<<eg<<": "<<S.size()<<endl;
S.clear();
}
}