Official

C - Secret Number Editorial by en_translator


This problem can be solved by, for each of \(10^4\) PINS from 0000 through 9999, check if it matches what Takahashi remembers, and count the number of matching ones.

When implementing, one can regard the PINS from 0000 through 9999 as “the decimal notations of integers from \(0\) through \(9999\) with leading zeros.” For example, the PIN corresponding to \(1\) is, with leading zeros, 0001; the PIN for the integer \(132\) is 0132 when leading zeros are prepended.

The complexity is \(O(M|S|)\), where \(M=10^4\).

Sample Code (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
    string S; cin >> S;
    int ans = 0;
    for(int i=0; i<=9999; i++){
        vector<bool> flag(10);
        int X = i;
        for(int j=0; j<4; j++){
            flag[X%10] = true;
            X /= 10;
        }
        bool flag2 = true;
        for(int j=0; j<10; j++){
            if(S[j]=='o' && !flag[j]) flag2 = false;
            if(S[j]=='x' && flag[j]) flag2 = false;
        }
        ans += flag2;
    }
    cout << ans << endl;
}

Sample Code (Python)

S = input()
ans = 0
for i in range(10000):
    flag = [False]*10
    now = i
    for j in range(4):
        flag[now%10] = True
        now //= 10
    flag2 = True
    for j in range(10):
        if S[j] == 'o' and not flag[j]:
            flag2 = False
        if S[j] == 'x' and flag[j]:
            flag2 = False
    ans += flag2
print(ans)

posted:
last update: