Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 10;
int n, f[10];
char s[N];
int main() {
scanf("%d\n%s", &n, s);
for(int i = 1; i <= 9; ++i)
scanf("%d", f + i);
bool conv = false;
for(int i = 0; s[i] != '\0'; ++i) {
if((conv && f[s[i] - '0'] >= s[i] - '0') || (!conv && f[s[i] - '0'] > s[i] - '0'))
conv = true;
if(conv && f[s[i] - '0'] < s[i] - '0')
break;
if(conv)
s[i] = f[s[i] - '0'] + '0';
}
puts(s);
return 0;
}
Copy The Code &
Try With Live Editor
Input
4
1337
1 2 5 4 6 6 3 1 9
1337
1 2 5 4 6 6 3 1 9
Output
1557
Demonstration
Codeforces Solution Long Number, B. Long Number-Solution in C, C++, Java, Python