1654B - Prefix Removals - CodeForces Solution


strings

Please click on ads to support us..

Python Code:

from math import inf
from collections import *
import math, os, sys, heapq, bisect, random
from functools import lru_cache
from itertools import *
def inp(): return sys.stdin.readline().rstrip("\r\n")
def out(var): sys.stdout.write(str(var))  def inpu(): return int(inp())
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def fsep(): return map(float, inp().split())
M,M1=1000000007,998244353

def main():
    how_much_noob_I_am = 1
    how_much_noob_I_am = inpu()
    for i in range(how_much_noob_I_am):
        s=inp()
        ss=set()
        d=defaultdict(int)
        for i in s:
            d[i]+=1
        ans=""
        for i in range(len(s)):
            d[s[i]]-=1
            if d[s[i]]==0:
                break
        print(s[i:])
if __name__ == '__main__':
    main()

C++ Code:

#include <bits/stdc++.h>

#include <random>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
//#define int long long
//#define double long double
typedef long long ll;
typedef unsigned long long ull;
#define fir first
#define sec second
#define pb push_back
#define eb emplace_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define sz(v) (int)(v).size()
//typedef tree<int, null_type, less_equal<>, rb_tree_tag,
//      tree_order_statistics_node_update> indexed_multiset;
const int inf = 1000000000;
const ll INF = (ll)inf * (ll)inf;
const double EPS = 1e-9;
const double PI = 3.141592653;
mt19937_64 Rand(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());

void solve()
{
    string s; cin >> s;
    vector <int> p(26, inf);
    for (int i = 0; i < sz(s); ++i)
    {
        p[s[i]-'a'] = i;
    }
    int mn = sz(s)-1;
    for (int i = 0; i < 26; ++i)
        mn = min(mn, p[i]);
    cout << s.substr(mn, sz(s) - mn) << '\n';
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int tt = 1; cin >> tt;
    while (tt--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language