Problem A
Concatenation of Languages
Input File: Standard Input
Output: Standard Output
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.
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.
2 3 2 cat dog mouse rat bat 1 1 abc cab |
Case 1: 6 Case 2: 1
|
题目大意:将A集合的词与B集合中的词按要求合成一个词,然后利用哈希表判重,问这个集合一共又几个元素。
解析:直接保存在set中,然后输出set的个数,虽然比较慢,但是代码简单。
注意:空串也算,所以要用gets或者getline输入。
#include <stdio.h> #include <iostream> #include <string> #include <set> using namespace std; const int N = 1505; string fir[N]; string sec[N]; int main() { int t; int m,n; int cas = 1; set<string> hash; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); getchar(); for(int i = 0; i < m; i++) { getline(cin,fir[i]); } for(int i = 0; i < n; i++) { getline(cin,sec[i]); } for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { string tmp = fir[i] + sec[j]; hash.insert(tmp); } } printf("Case %d: %d\n",cas++,(int)hash.size()); hash.clear(); } return 0; }