Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define ll long long
- #define ld double
- #define llu long long unsigned
- bool comp( string x , string y )
- {
- for( int i = 0 ; i < min( x.size() , y.size() ) ; i++ )
- {
- if( x[i] >= 'A' and x[i] <= 'Z' and y[i] >= 'a' and y[i] <= 'z' and y[i] - 'a' < x[i] - 'A' )
- return 1;
- else if( x[i] >= 'A' and x[i] <= 'Z' and y[i] >= 'a' and y[i] <= 'z' )
- return 0;
- else if( y[i] >= 'A' and y[i] <= 'Z' and x[i] >= 'a' and x[i] <= 'z' and x[i] - 'a' < y[i] - 'A' )
- return 0;
- else if( y[i] >= 'A' and y[i] <= 'Z' and x[i] >= 'a' and x[i] <= 'z' )
- return 1;
- else if( y[i] < x[i] )
- return 1;
- else if( x[i] < y[i] )
- return 0;
- }
- }
- void merge(vector < string > &arr, int l, int m, int r)
- {
- int i, j, k;
- int n1 = m - l + 1;
- int n2 = r - m;
- string L[n1], R[n2];
- for (i = 0; i < n1; i++)
- L[i] = arr[l + i];
- for (j = 0; j < n2; j++)
- R[j] = arr[m + 1+ j];
- i = 0;
- j = 0;
- k = l;
- while (i < n1 && j < n2)
- {
- if ( comp( R[j] , L[i] ) )
- {
- arr[k] = L[i];
- i++;
- }
- else
- {
- arr[k] = R[j];
- j++;
- }
- k++;
- }
- while (i < n1)
- {
- arr[k] = L[i];
- i++;
- k++;
- }
- while (j < n2)
- {
- arr[k] = R[j];
- j++;
- k++;
- }
- }
- void mergeSort(vector < string > &arr, int l, int r)
- {
- if (l < r)
- {
- int m = l+(r-l)/2;
- mergeSort(arr, l, m);
- mergeSort(arr, m+1, r);
- merge(arr, l, m, r);
- }
- }
- int main()
- {
- fast;
- int tt;
- cin>>tt;
- for( int i = 1 ; i <= tt ; i++ )
- {
- int n;
- cin>>n;
- vector < string > a;
- vector < string > b;
- vector < string > c;
- vector < string > d;
- while( n-- )
- {
- string s;
- cin>>s;
- if( s[0] == 'A' or s[0] == 'a' )
- a.push_back( s );
- if( s[0] == 'B' or s[0] == 'b' )
- b.push_back( s );
- if( s[0] == 'C' or s[0] == 'c' )
- c.push_back( s );
- if( s[0] == 'D' or s[0] == 'd' )
- d.push_back( s );
- }
- // sort( a.begin() , a.end() );
- mergeSort( a , 0 , a.size() - 1 );
- // sort( b.begin() , b.end() );
- mergeSort( b , 0 , b.size() - 1 );
- // sort( c.begin() , c.end() );
- mergeSort( c , 0 , c.size() - 1 );
- // sort( d.begin() , d.end() );
- mergeSort( d , 0 , d.size() - 1 );
- // for( int i = 0 ; i < a.size() ; i++ )
- // cout<<a[i]<<endl;
- // for( int i = 0 ; i < b.size() ; i++ )
- // cout<<b[i]<<endl;
- // for( int i = 0 ; i < c.size() ; i++ )
- // cout<<c[i]<<endl;
- // for( int i = 0 ; i < d.size() ; i++ )
- // cout<<d[i]<<endl;
- cin>>n;
- for( int i = 0 ; i <= n ; i++ )
- {
- string s;
- // getchar();
- getline( cin , s );
- if( !i )
- continue;
- // cout<<s<<endl;
- if( s[0] == 'A' and a.size() )
- {
- cout<<a[0]<<endl;
- a.erase( a.begin() );
- }
- else if( s[0] == 'B' and b.size() )
- {
- cout<<b[0]<<endl;
- b.erase( b.begin() );
- }
- else if( s[0] == 'C' and c.size() )
- {
- cout<<c[0]<<endl;
- c.erase( c.begin() );
- }
- else if( s[0] == 'D' and d.size() )
- {
- cout<<d[0]<<endl;
- d.erase( d.begin() );
- }
- else
- cout<<"Already Mastered"<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement