Untitled
unknown
plain_text
a year ago
3.6 kB
9
Indexable
#include <bits/stdc++.h>
using namespace std;
#define int long long
void __print(int x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
void solve() {
int n;
cin >> n;
vector <int> a;
for(int i=0;i<n;i++){
int x;
cin >> x;
a.push_back(x);
}
int m;
cin >> m;
vector<vector<pair<int,char>>> c;
for(int i=0;i<m;i++){
vector<pair<int,char>> b;
string s;
cin >> s;
int flag = 0;
if(s.size()!=n){
flag = 1;
}
for(int j=0;j<s.size();j++){
if(flag==1){
b.push_back({-1,s[j]});
}
else{
b.push_back({a[j],s[j]});
}
}
c.push_back(b);
}
//copy the vector of pairs c to a new vector of pairs d
vector<vector<pair<int,char>>> d = c;
for(int i=0;i<m;i++){
//sort the vector of pairs c[i] according to the first element of the pair
sort(c[i].begin(), c[i].end(), [](const pair<int,char>& v1, const pair<int,char>& v2) {
return v1.first < v2.first; // Lexicographical comparison
});
//sort the vector of pairs d[i] according to the second element of the pair
sort(d[i].begin(), d[i].end(), [](const pair<int,char>& v1, const pair<int,char>& v2) {
return v1.second < v2.second; // Lexicographical comparison
});
}
debug(c);
debug(d);
for(int i=0;i<m;i++){
if(c[i][0].first==-1){
cout << "NO" << endl;
continue;
}
int flag = 0;
for(int j=0;j<c[i].size()-1;j++){
if(c[i][j].first==c[i][j+1].first){
if(c[i][j].second!=c[i][j+1].second){
cout << "NO" << endl;
flag = 1;
break;
}
}
if(c[i][j].first!=c[i][j+1].first){
if(c[i][j].second==c[i][j+1].second){
cout << "NO" << endl;
flag = 1;
break;
}
}
if(d[i][j].second==d[i][j+1].second){
if(d[i][j].first!=d[i][j+1].first){
cout << "NO" << endl;
flag = 1;
break;
}
}
if(d[i][j].second!=d[i][j+1].second){
if(d[i][j].first==d[i][j+1].first){
cout << "NO" << endl;
flag = 1;
break;
}
}
}
if(flag==0){
cout << "YES" << endl;
}
}
}
signed main() {
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}Editor is loading...
Leave a Comment