Dev Skill Coin puzzle
//Next Codeforces Round #354 (Div. 2)
#include<bits/stdc++.h>
//#include<cstdio>
//#include<iostream>
//#include<algorithm>
//#include<vector>
//#include<cstring>
//#include<cmath>
//#include<map>
using namespace std;
#define fast ios_base::sync_with_stdio(false)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
//#define i64 long long
#define high 30
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<char>vc;
typedef map<char, int>mpci;
typedef set<char>sch;
struct bmy
{
char chboro;
int bcnt;
};
bmy boro[30];
struct cmy
{
char chchoto;
int ccnt;
};
cmy choto[high];
bool cmpboro(bmy a , bmy b)
{
return a.bcnt > b.bcnt;
}
bool cmpchoto(cmy a , cmy b)
{
return a.ccnt < b.ccnt;
}
int main()
{
fast;
int t , n , q , blen=0, clen=0;
mpci mpboro, mpchoto, mark;
string s, ans="";
cin >> t;
while(t--)
{
mpboro.clear();
mpchoto.clear();
mark.clear();
ans="";
cin >> n >> q;
blen=clen=0;
while(q--)
{
cin >> s;
if(s[1] == '>')
{
if(mpboro.count(s[0]) == 0)
{
mpboro[s[0]]++;
boro[blen].chboro = s[0];
blen++;
}
else
{
mpboro[s[0]]++;
}
if(mpchoto.count(s[2]) == 0)
{
mpchoto[s[2]]++;
choto[clen].chchoto = s[2];
clen++;
}
else
{
mpchoto[s[2]]++;
}
}
else
{
if(mpboro.count(s[2]) == 0)
{
mpboro[s[2]]++;
boro[blen].chboro = s[2];
blen++;
}
else
{
mpboro[s[2]]++;
}
if(mpchoto.count(s[0]) == 0)
{
mpchoto[s[0]]++;
choto[clen].chchoto = s[0];
clen++;
}
else
{
mpchoto[s[0]]++;
}
}
}
for(int i=0; i<blen; i++)
{
boro[i].bcnt = mpboro[boro[i].chboro];
}
for(int i=0; i<clen; i++)
{
choto[i].ccnt = mpchoto[choto[i].chchoto];
}
sort(boro, boro+blen, cmpboro);
sort(choto, choto+clen, cmpchoto);
for(int i=0; i<blen; i++)
{
if(mark.count(boro[i].chboro) == 0)
{
ans+=boro[i].chboro;
mark[boro[i].chboro] = 1;
}
}
for(int i=0; i<clen; i++)
{
if(mark.count(choto[i].chchoto) == 0)
{
ans+=choto[i].chchoto;
mark[choto[i].chchoto] = 1;
}
}
//for(int i=0; i<blen; i++) cout << boro[i].chboro << " " << boro[i].bcnt << "\n";
//for(int i=0; i<clen; i++) cout << choto[i].chchoto << " " << choto[i].ccnt << "\n";
bool f=true;
for(int i=0; i<blen; i++)
{
if(mpboro[boro[i].chboro] == mpchoto[choto[i].chchoto])
{
f=false;
}
else
{
f=true;
break;
}
}
for(int i=0; i<clen; i++)
{
if(mpchoto[choto[i].chchoto] == mpboro[boro[i].chboro])
{
f=false;
}
else
{
f=true;
break;
}
}
if(f)
{
reverse(ans.begin(), ans.end());
outn(ans);
}
else
{
outn("Impossible");
}
}
return 0;
}
#include<bits/stdc++.h>
//#include<cstdio>
//#include<iostream>
//#include<algorithm>
//#include<vector>
//#include<cstring>
//#include<cmath>
//#include<map>
using namespace std;
#define fast ios_base::sync_with_stdio(false)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
//#define i64 long long
#define high 30
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<char>vc;
typedef map<char, int>mpci;
typedef set<char>sch;
struct bmy
{
char chboro;
int bcnt;
};
bmy boro[30];
struct cmy
{
char chchoto;
int ccnt;
};
cmy choto[high];
bool cmpboro(bmy a , bmy b)
{
return a.bcnt > b.bcnt;
}
bool cmpchoto(cmy a , cmy b)
{
return a.ccnt < b.ccnt;
}
int main()
{
fast;
int t , n , q , blen=0, clen=0;
mpci mpboro, mpchoto, mark;
string s, ans="";
cin >> t;
while(t--)
{
mpboro.clear();
mpchoto.clear();
mark.clear();
ans="";
cin >> n >> q;
blen=clen=0;
while(q--)
{
cin >> s;
if(s[1] == '>')
{
if(mpboro.count(s[0]) == 0)
{
mpboro[s[0]]++;
boro[blen].chboro = s[0];
blen++;
}
else
{
mpboro[s[0]]++;
}
if(mpchoto.count(s[2]) == 0)
{
mpchoto[s[2]]++;
choto[clen].chchoto = s[2];
clen++;
}
else
{
mpchoto[s[2]]++;
}
}
else
{
if(mpboro.count(s[2]) == 0)
{
mpboro[s[2]]++;
boro[blen].chboro = s[2];
blen++;
}
else
{
mpboro[s[2]]++;
}
if(mpchoto.count(s[0]) == 0)
{
mpchoto[s[0]]++;
choto[clen].chchoto = s[0];
clen++;
}
else
{
mpchoto[s[0]]++;
}
}
}
for(int i=0; i<blen; i++)
{
boro[i].bcnt = mpboro[boro[i].chboro];
}
for(int i=0; i<clen; i++)
{
choto[i].ccnt = mpchoto[choto[i].chchoto];
}
sort(boro, boro+blen, cmpboro);
sort(choto, choto+clen, cmpchoto);
for(int i=0; i<blen; i++)
{
if(mark.count(boro[i].chboro) == 0)
{
ans+=boro[i].chboro;
mark[boro[i].chboro] = 1;
}
}
for(int i=0; i<clen; i++)
{
if(mark.count(choto[i].chchoto) == 0)
{
ans+=choto[i].chchoto;
mark[choto[i].chchoto] = 1;
}
}
//for(int i=0; i<blen; i++) cout << boro[i].chboro << " " << boro[i].bcnt << "\n";
//for(int i=0; i<clen; i++) cout << choto[i].chchoto << " " << choto[i].ccnt << "\n";
bool f=true;
for(int i=0; i<blen; i++)
{
if(mpboro[boro[i].chboro] == mpchoto[choto[i].chchoto])
{
f=false;
}
else
{
f=true;
break;
}
}
for(int i=0; i<clen; i++)
{
if(mpchoto[choto[i].chchoto] == mpboro[boro[i].chboro])
{
f=false;
}
else
{
f=true;
break;
}
}
if(f)
{
reverse(ans.begin(), ans.end());
outn(ans);
}
else
{
outn("Impossible");
}
}
return 0;
}
Comments
Post a Comment