constructive algorithms dp greedy strings

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

using namespace std;


#define take(x) int x;cin>>x;
#define give(x) cout <<x<<nline;
#define tvec(x,n) for (int i = 0; i < n; i++){cin >>x[i]; }
#define tvec1(x,n) for (int i = 1; i <= n; i++){cin >>x[i]; }
#define gvec(x,n) for (int i = 0; i < n; i++){cout <<x[i]<<" "; }cout<<nline;
#define gvec1(x,n) for (int i = 1; i <= n; i++){cout <<x[i]<<" "; }cout<<nline;
#define each(a, b) for (auto&(a) : (b))
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define mod1 998244353
#define inf 1e18
#define nline "\n"
#define LB lower_bound
#define UB upper_bound
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define setbits __builtin_popcountll
#define frontzero __builtin_clzll 
#define backzero __builtin_ctzll
#define parity __builtin_parityll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define reset(a, b) memset(a, b, sizeof(a))
template<typename S,typename T> void smax(S &a, const T &b) { if (a<b) a=b;}
template<typename S,typename T> void smin(S &a, const T &b) { if (a>b) a=b;}

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T> void _print(T arr[]);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
int mindfuk=1;
template <class T> void _print(T arr[]){cerr << "[ "; for (int i=0;i<mindfuk ;i++) {_print(arr[i]); cerr << " ";} cerr << "]";}



void comeon(){
    take(n);take(k);take(m);
    string s; cin>>s;
    int total=0;
    int cnt=0;string ans;
    int alpha[k];memset(alpha,0,sizeof(alpha));
    for (int i = 0; i < m; i++)
    {
        if(alpha[s[i]-'a']==0){
            cnt++;alpha[s[i]-'a']=1;
        }
        if(cnt==k){ans.push_back(s[i]);debug(i);
            total++;cnt=0;memset(alpha,0,sizeof(alpha));
        }
    }debug(ans);debug(total);
    if(total>=n){
        cout<<"yes"<<nline;return;
    }
    for (int i = 0; i < k; i++)
    {
        if(alpha[i]==0){char xx='a'+i;
            for (int j = 0; j < n-total; j++)
            {
                ans.pb(xx);
            } break;
        }
       
    }
give("no");give(ans);
    
}

int main() {
    fastio();
    
    int TC =1;
    cin >> TC;
    while(TC--) comeon();

}


Comments

Submit
0 Comments
More Questions

1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare