#include <bits/stdc++.h>
using
namespace
std;
class
SpecialInterval {
public
:
int
low, high;
SpecialInterval(
int
low,
int
high)
{
this
->low = low;
this
->high = high;
}
};
bool
checkChar(string str)
{
bool
hash[26] = {
false
};
for
(
int
i = 0; i < str.length(); i++) {
hash[str[i] -
'a'
] =
true
;
}
return
*hash;
}
bool
checkIfValid(vector<SpecialInterval> intervals,
vector<
int
> arr)
{
if
(intervals.size() == 0) {
return
true
;
}
sort(intervals.begin(), intervals.end(),
[](SpecialInterval a, SpecialInterval b) {
return
a.low < b.low;
});
priority_queue<
int
, vector<
int
>, greater<
int
> > pq;
int
count = 0, j = 0;
for
(
int
i = 0;
i < arr.size() && count < intervals.size(); i++) {
while
(j < intervals.size()) {
SpecialInterval interval = intervals[j];
if
(interval.low > arr[i])
break
;
j++;
pq.push(interval.high);
}
if
(pq.size() > 0) {
int
high = pq.top();
pq.pop();
if
(arr[i] > high) {
break
;
}
count++;
}
}
return
count == intervals.size();
}
vector<SpecialInterval> findSpecialIntevals(string str,
char
ch)
{
int
oddPrev = -1;
int
evenPrev = -1;
vector<SpecialInterval> intervals;
for
(
int
i = 0; i < str.length(); i++) {
if
(str[i] == ch) {
if
(i % 2 == 0) {
if
(evenPrev == -1) {
evenPrev = i;
}
else
{
SpecialInterval interval(evenPrev, i);
intervals.push_back(interval);
evenPrev = -1;
}
}
else
{
if
(oddPrev == -1) {
oddPrev = i;
}
else
{
SpecialInterval interval(oddPrev, i);
intervals.push_back(interval);
oddPrev = -1;
}
}
}
}
return
intervals;
}
void
printAnswer(vector<
char
> str, vector<
int
> arr)
{
sort(arr.begin(), arr.end());
bool
possible =
false
;
for
(
int
ch = 0; ch < 26; ch++) {
if
(!checkChar(string(str.begin(), str.end())))
continue
;
vector<SpecialInterval> intervals
= findSpecialIntevals(
string(str.begin(), str.end()), ch +
'a'
);
possible = checkIfValid(intervals, arr);
if
(possible) {
cout <<
char
(ch +
'a'
) << endl;
break
;
}
}
if
(!possible)
cout << -1 << endl;
}
int
main()
{
vector<
char
> str
= {
'a'
,
'b'
,
'c'
,
'a'
,
'e'
,
'a'
,
'a'
};
vector<
int
> arr = { 4, 6 };
printAnswer(str, arr);
return
0;
}