题意:集合S1和S2,把S2中的元素拼接到S1的后面去,生成新的元素,问有多少个不重复的元素
直接map.注意,不能用cin读取字符串,题目没有保证字符串中间没有空格
#include "pch.h" #include <string> #include<iostream> #include<map> #include<memory.h> #include<vector> namespace cc { using std::cout; using std::endl; using std::cin; using std::map; using std::vector; using std::string; int n; int t; int r, c; int total; constexpr int N = 1501; string set1[N]; string set2[N]; map<string, int>allMaps; void cal() { for (int i = 0;i < r;i++) { for (int j = 0;j < c;j++) { string str = set1[i] + set2[j]; if (allMaps[str] == 0) { ++total; allMaps[str] = 1; } } } } void read() { total = 0; allMaps.clear(); cin >> r >> c; getchar(); for (int i = 0;i < r;i++) getline(cin,set1[i]); for (int i = 0;i < c;i++) getline(cin, set2[i]); } void solve() { t = 1; cin >> n; while (n--) { read(); cal(); cout << "Case " << t << ": " << total << endl; t++; } } }; int main() { #ifndef ONLINE_JUDGE freopen("d://1.text", "r", stdin); #endif // !ONLINE_JUDGE cc::solve(); return 0; }