一道题

题目描述:
有的字符串是相似的,比如”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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值