You are given a long decimal number a consisting of n digits from 1 to 9. You also have a function f that maps every digit from 1 to 9 to some (possibly the same) digit from 1 to 9.
You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in a, and replace each digit x from this segment with f(x). For example, if a=1337, f(1)=1, f(3)=5, f(7)=3, and you choose the segment consisting of three rightmost digits, you get 1553 as the result.
What is the maximum possible number you can obtain applying this operation no more than once?
Input
The first line contains one integer n (1≤n≤2⋅105) — the number of digits in a.
The second line contains a string of n characters, denoting the number a. Each character is a decimal digit from 1 to 9.
The third line contains exactly 9 integers f(1), f(2), …, f(9) (1≤f(i)≤9).
Output
Print the maximum number you can get after applying the operation described in the statement no more than once.
Examples
inputCopy
4
1337
1 2 5 4 6 6 3 1 9
outputCopy
1557
inputCopy
5
11111
9 8 7 6 5 4 3 2 1
outputCopy
99999
inputCopy
2
33
1 1 1 1 1 1 1 1 1
outputCopy
33
使变幻出来的数最大,可以连续用,但是中间有一个数不用的时候后面就都不能用了
先判断一下,如果前面的数就小,就从下一个开始看,因为这时候我们还没用过变换的能力
最后输出就OK了
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<vector>
#define ll long long
#define dd double
using namespace std;
int main() {
ll n;
while (cin >> n) {
ll a[15];
string s;
vector<ll> s1;
cin >> s;
for (ll i = 0; i < 9; i++) {
cin >> a[i];
}
ll position = -1;
ll flag = 0;
for (ll i = 0; i < s.size(); i++) {
if ((s[i] - '0') >= a[(s[i] - '0') - 1] && flag == 0) {
s1.push_back((s[i]) - '0');
}
else if ((s[i] - '0') <= a[(s[i] - '0') - 1]) {
flag = 1;
s1.push_back(a[s[i] - '0' - 1]);
}
else {
position = i;
break;
}
}
if (position != -1) {
for (ll i = position; i < s.size(); i++) {
s1.push_back((s[i])-'0');
}
}
for (ll i = 0; i < s1.size(); i++) {
cout << s1[i];
}
cout << endl;
}
}