题目描述:
有的字符串是相似的,比如”AABBBC”和”DDXXXZ”,或者”ABAB”和”BABA”。我们定义
这种相似为:如果可以找到一个字符间的一对一映射,将一个串转化为另一个串,那么我们就
认为这两个字符串是相似的。比如第一个例子中,{A->D, B->X, C->Z}就是一个能满足条件的
映射,再比如AABB 和BBCC 是相似的,但是AABB 和AAAA 不是相似的。
现在有N 个字符串需要进行筛选,留下的字符串必须两两相似,问最多能留下多少个字符
串。
输入
第一行是一个整数N (1 <= N <= 1000)
接下来是N 行,每行一个字符串,均为大写字母且长度不超过1000。
输出
最多留下的字符串个数
输入样例
6
HULU
GURU
JAVA
YAHOO
GOOGLE
AMAZON
输出样例
3
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_map>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <numeric>
using namespace std;
string strHash(string & s) {
vector<char> transform(26, '.');
int len = s.length();
string res;
int cur = 0;
for (char c : s) {
if (transform[c - 'A'] == '.') {
transform[c - 'A'] = static_cast<char>('A' + cur);
res += transform[c - 'A'];
++cur;
}
else {
res += transform[c - 'A'];
}
}
return res;
}
int main() {
int n;
while (cin >> n) {
string tmp;
unordered_map<string, int> count;
for (int i = 0; i < n; ++i) {
cin >> tmp;
string key = strHash(tmp);
if (count.find(key) == count.end()) {
count[key] = 1;
}
else {
++count[key];
}
}
int res = 0;
for (auto it = count.begin(); it != count.end(); ++it) {
if (it->second > 1) {
res = max(res, it->second);
}
}
cout << res << endl;
}
return 0;
}