Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define __GLIBCXX_DEBUG
- #include <bits/stdc++.h>
- using namespace std;
- #define REP(i, n) for(int i = 0; i < (int)(n); ++i)
- #define pb push_back
- #define ALL(a) (a.begin()), (a.end())
- #define mp make_pair
- #define F first
- #define S second
- #define SZ(a) ((int)(a.size()))
- typedef long long ll;
- typedef pair<int, int> PI;
- int main(int argc, char *argv[])
- {
- int n;
- while (cin >> n && n){
- string S;
- cin >> S;
- int dp[11][11][1 << 10];
- memset(dp, 0, sizeof(dp));
- bool unsafe = false;
- REP(i, n) if (i == 0 || S[i - 1] == 'u'){
- int s = i;
- if (S[s] == 'u' || S[s + 1] == 'u') continue;
- // cout << s << endl;
- int mask = 0;
- while (S[s] != 'u'){
- if (mask & (1 << (S[s] - '0'))){
- unsafe = true;
- } else {
- // cout << s << " " << S[s] << " " << mask << endl;
- dp[0][S[s] - '0'][mask] = 1;
- }
- mask |= 1 << (S[s] - '0');
- s++;
- }
- }
- REP(t, 10) if (t > 0){
- REP(d, 10) REP(mask, 1 << 10) if (dp[t - 1][d][mask]){
- REP(i, n - 1) if (S[i] == 'u' && S[i + 1] != 'u'){
- int n_mask = 0;
- int p = i + 1;
- while (S[p] != 'u' && (mask & (1 << (S[p] - '0'))) == 0){
- n_mask |= 1 << (S[p] - '0');
- p++;
- }
- // cout << t << " " << d << " " << mask << " " << p << " " << n_mask << endl;
- if (S[p] != 'u'){
- if ((n_mask | mask) & (1 << d)) unsafe = true;
- dp[t][d][mask | n_mask] = 1;
- }
- }
- }
- }
- if (unsafe) {
- cout << "UNSAFE" << endl;
- } else {
- cout << "SAFE" << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement