Project Euler Problem 79: Passcode derivation【拓扑排序】

PE其他解题报告请参考这里,本题答案在留言首条

Passcode derivation

Problem 79

A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was 531278, they may ask for the 2nd, 3rd, and 5th characters; the expected reply would be: 317.

The text file, keylog.txt, contains fifty successful login attempts.

Given that the three characters are always asked for in order, analyse the file so as to determine the shortest possible secret passcode of unknown length.


题意:

让你求尽可能短的密码段,使得每个三位的密码,都可以依次取出

分析:

很明显这是一个拓扑排序的题,比如一个数字,123,只需要保证1在2前,1在3前,2在3前,因为不知道原先都有哪些数字出现过,所以我这里用了一个dig数组来存储出现过的数字,然后拓扑排序的板子即可

参考代码
#include<bits/stdc++.h>

using namespace std;

#define pb push_back

int ind[11];
vector<int> E[11];
bool dig[11];

int main(){
    for (int i = 0; i < 50; i++) {
        int x; cin >> x;
        int a = x / 100, b = x / 10 % 10, c = x % 10;
        dig[a] = dig[b] = dig[c] = 1;
        E[a].pb(b), E[a].pb(c), E[b].pb(c);
        ind[b]++, ind[c] += 2;
    }
    int cnt = 0;
    for (int i = 0; i < 10; i++) if (dig[i]) cnt++;
    string s;
    while (cnt) {
        int t = -1;
        for (int i = 0; i < 10; i++) {
            if (!ind[i] && dig[i]) {
                dig[i] = 0;
                cnt--;
                t = i;break;
            }
        }
        if (t == -1) {
            puts("No answer");return 0;
        }
        s += (char)('0' + t);
        for (auto it : E[t]) ind[it]--;
    }
    cout << s << endl;
    return 0;
}

阅读好习惯:点赞 + 收藏~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值