Official
B - cat 2 Editorial by en_translator
Repeated strings can be removed by using an associated array (like a balanced binary search tree or a hash map).
The problem can be solved by enumerating \((i,j)\) using a for statement, and inserting the concatenation of the strings \(S _ i\) and \(S _ j\) into an associated array.
The following is sample code.
Sample code in C++
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main() {
int N;
cin >> N;
vector<string> S(N);
for (auto&& s : S)
cin >> s;
// Use `std::set` to remove duplicating strings
set<string> ans;
for (const auto& s : S)
for (const auto& t : S)
if (s != t) // If two strings are different
ans.emplace(s + t); // concatenate them and insert
// The answer is the size of the set
cout << size(ans) << endl;
return 0;
}
Sample code in Python
N = int(input())
S = [input() for i in range(N)]
# Use `set` to remove duplicating strings
ans = set()
for s in S:
for t in S:
if s != t: # If two strings are different
ans.add(s + t) # concatenate them and insert
# The answer is the size of the set
print(len(ans))
posted:
last update: