brute force constructive algorithms strings *1200

Please click on ads to support us..

Python Code:

def intToChar(x):     return chr(ord('a')+x)

allsubstrings = []
for mask in range(26):
    arr = [mask]
    ss = ''.join(intToChar(x) for x in arr)
    allsubstrings.append(ss)
for mask in range(26 ** 2):
    arr = []
    for _ in range(2):
        arr.append(mask % 26)
        mask //= 26
    arr.reverse()
    ss = ''.join(intToChar(x) for x in arr)
    allsubstrings.append(ss)
for mask in range(26 ** 3):
    arr = []
    for _ in range(3):
        arr.append(mask % 26)
        mask //= 26
    arr.reverse()
    ss = ''.join(intToChar(x) for x in arr)
    allsubstrings.append(ss)

allsubstrings = allsubstrings[: 3000]

def main():
    
    t = int(input())
    allans = []
    for _ in range(t):
        n = int(input())
        s = input()
        substrings = set()
        for i in range(n):
            substrings.add(s[i])
            if i + 1 < n:
                substrings.add(s[i: i + 2])
            if i + 2 < n:
                substrings.add(s[i: i + 3])
        for ss in allsubstrings:
            if ss not in substrings:
                ans = ss
                break
        allans.append(ans)
    multiLineArrayPrint(allans)
    
    return

import sys
input=lambda: sys.stdin.readline().rstrip("\r\n")  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
// #define ll long long
#define f(i,n) for(int i=0;i<n;i++)
#define fi(i,x,n) for(int i=x;i<n;i++)
#define pii pair<int,int>
#define vi vector<int>
#define pb push_back
#define all(x)            (x).begin(),(x).end()
#define rof(i,a,b) for(int i=(a);i>=(b);i--)
#define S second
#define F first
#define is insert
#define sz(a) (int)(a.size())

void solve() {

    int n; cin >> n;
    string s;
    cin >> s;
    bool ans = false;
    int count[26] = {0};
    for (int i = 0; i < n; i++) {
        count[s[i] - 'a'] = 1;
    }

    for (int i = 0; i < 26; i++) {
        if (count[i] == 0) {
            cout << (char)('a' + i) << '\n';
            return;
        }
    }
    {
        set<string>st;
        for (int i = 0; i < n - 1; i++) {
            string temp = "";
            temp += s[i];
            temp += s[i + 1];
            st.insert(temp);
        }


        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < 26; j++) {
                string temp = "";
                temp += (char)('a' + i);
                temp += (char)('a' + j);
                if (st.find(temp) == st.end()) {
                    cout << temp << "\n";
                    return;
                }
            }
        }
    }
    {
        set<string>st;
        for (int i = 0; i < n - 2; i++) {
            string temp = "";
            temp += s[i];
            temp += s[i + 1];
            temp += s[i + 2];
            st.insert(temp);
        }


        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < 26; j++) {
                string temp = "a";
                temp += (char)('a' + i);
                temp += (char)('a' + j);
                if (st.find(temp) == st.end()) {
                    cout << temp << "\n";
                    return;
                }
            }
        }
    }

}

int32_t main() {
#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
#endif

    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        //cout << "\n";
    }
}


Comments

Submit
0 Comments
More Questions

1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort