【AtCoder】abc_198D - Send More Money【next_permutation暴力模拟】

题目链接
题意:给你三个小写字母组成的字符串 A , B , C A,B,C A,B,C,给每个字母代表一个数字( [ 0 , 9 ] [0,9] [0,9]),问能否找到一个组合使得 A , B , C A,B,C A,B,C对应转换为数字之后满足 A + B = C A+B=C A+B=C,例如:
A = " s e n d " , B = " m o r e " , C = " m o n e y " A="send",B="more",C="money" A="send",B="more",C="money"
对应转换为数字
A = 9567 , B = 1085 , C = 10652 A=9567,B=1085,C=10652 A=9567,B=1085,C=10652
满足 A + B = C A+B=C A+B=C,则输出 A , B , C A,B,C A,B,C(字符串长度 ≤ 10 \le 10 10
思路:对于这个数据范围明显可以枚举,主要问题就是代码实现比较麻烦,可以使用DFS也可以map暴力,不过这里采用一种比较好写的方法:首先简单判断一下三个字符串中总的不同字母数量,如果超过10显然直接无解;否则枚举每一个字母代表的数,注意处理前缀0和1e10的数据范围(需要开long long),枚举过程通过next_permutation实现,它能够遍历一个数组的所有排列,于是用它来遍历 { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } \{0,1,2,3,4,5,6,7,8,9\} {0,1,2,3,4,5,6,7,8,9}的所有排列,而字母用来对应下标,这样代码实现就会很简单很舒适啦。
AC代码:

#include <bits/stdc++.h>
#define pcc pair<char, char>
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, y) for (int i = x; i < y; i++)
#define per(i, x, y) for (int i = x; i >= y; i--)
#define rep0(i, n) for (int i = 0; i < (n); i++)
#define per0(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ull unsigned long long
#define db double
#define ld long double
using namespace std;

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

const double eps = 1e-9;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 6e6 + 10;
const int MAX = 1e5;
const double pi = acos(-1.0);
const ll INF = 0x3f3f3f3f3f3f3f;
ll qpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) res = (res * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return res % mod;
}
/***************main****************/
vector<ll> v(26);
int main() {
    string a, b, c, St;
    cin >> a >> b >> c;
    St = a + b + c;
    for (char i : St) {
        v[i - 'a']++;
    }
    map<ll, ll> ma;
    ll cnt = 0;
    for (ll i = 0; i < 26; i++) {
        if (v[i]) ma[i] = cnt++;
    }
    if (cnt > 10) {
        cout << "UNSOLVABLE\n";
        return 0;
    }
    ll p[10];
    for (ll i = 0; i < 10; i++) p[i] = i;
    do {
        ll A = 0, B = 0, C = 0;
        bool f = 1;
        for (ll i = 0; i < a.size(); i++) {
            A = (p[ma[a[i] - 'a']]) + A * 10;
            if (i == 0 && p[ma[a[i] - 'a']] == 0) f = 0;
        }
        for (ll i = 0; i < b.size(); i++) {
            B = (p[ma[b[i] - 'a']]) + B * 10;
            if (i == 0 && p[ma[b[i] - 'a']] == 0) f = 0;
        }
        for (ll i = 0; i < c.size(); i++) {
            C = (p[ma[c[i] - 'a']]) + C * 10;
            if (i == 0 && p[ma[c[i] - 'a']] == 0) f = 0;
        }
        // cout << A << '\n' << B << '\n' << C << endl;
        if (f && A + B == C) {
            cout << A << '\n' << B << '\n' << C << '\n';
            return 0;
        }
    } while (next_permutation(p,p+10));
    cout << "UNSOLVABLE\n";
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值