Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя raghavvmittal11

Автор raghavvmittal11, история, 8 месяцев назад, По-английски

Intution — Here the intution is we create a map and store the chararcter and map it with there count. Example 0 0 1 1 now here firstly we encounter 0 now we check is there any character in the answer string if (yes) then we we increase its count in the map and if(no) then we ad the character into the string and increase its count in map and then move to the next character. now for 0 string become "a", now we encounter 0 again but we already move to the next character hence string become "ab" now intresting part come we encounter 1 then we check is are any character count maps the 1 we encounter and the answer is yes its "a" hence we add a to the string "aba" and increase its count and move to the next character.Now finally we again encounter the 1 and we have one character whose count maps the 1 and its b hence we add b to our ans string and its becomes "abab".and then we return our answer string.

Your code here...

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define endl "\n"
#define pb push_back
#define mod 1000000007
#define N 500010
#define inf 100000000000000000

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int testcases = 1;
    cin>>testcases;
    while (testcases--)
    {
        // 1. simply take input of testcases
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
        {
        	cin>>a[i];
        }
        
        // create a map to store the count correspond to the each character
        map<char,int>mp;
        string s="";
        //take the first element as a string 
        char ch='a';
        for(int i=0;i<n;i++)// for the count ==0 here it add new char and increament the map count for the character
        {
        	if(a[i]==0)
        	{
        		s.push_back(ch);
        		mp[ch]++;
        		ch++;// make the character increase its value 
        	}
        	
        	else
        	{
        	    // Now if the char occured once then check the count of the character from map and increase its count
        		for(char j='a';j<='z';j++)
        		{// check character in map (count)
        			if(mp[j]==a[i])
        			{ // insert the char into ans string
        				s.push_back(j);
        				mp[j]++;// increase its map count value
        				break;
        			}
        		}
        	}
        }
        cout<<s<<endl;// print the anser string
    }
}

  • Проголосовать: нравится
  • -17
  • Проголосовать: не нравится

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by raghavvmittal11 (previous revision, new revision, compare).