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;
}
阅读好习惯:点赞 + 收藏~