题目大意:有一个\(n\)位数,其中的数字只有\(1\)~\(9\),不包括\(0\),每个\(1\)~\(9\)的数字有一个映射,映射也在\(1\)~\(9\)中,现在我们可以对这个\(n\)位数进行一些操作:选取这个数的一个区间,将区间中的数字全部替换成他们的映射数字,问:在只进行一次操作的情况下,更改后的数字最大为多少?
吐槽一句:某度翻译把题目翻译成了“只让这个数中的一个数字的映射生效,最大值是多少”,害得窝卡了1小时。。。
其实这题很简单,我们不妨先把\(x\)的映射称为\(f(x)\),我们不断找\(x \le f(x)\)的在\(n\)位数中连续的\(x\),如果\(f(x)<x\),就退出寻找,但是,如果这样写,那么一定是错的,为什么呢?
比如说下面这个例子:
5
18337
1 2 5 4 6 6 3 1 9
如果按照上面的思路,得到的答案仍然是\(18337\),为什么呢?因为\(f(1)=1,f(8)=1\),所以在我们找到“\(8\)”的时候,就会直接退出寻找,因此,我们应该判断:如果有许多在\(n\)位数中连续的\(x\),它们都符合\(f(x)=x\),那么,这个子段就应该放弃寻找,去找另外的子段。
\(Code:\)
#pragma GCC diagnostic error "-std=c++11"
#include <cstdio>
using namespace std;
template<class T>void r(T &a)
{
T s=0,w=1;a=0;char ch=getc(stdin);
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getc(stdin);}
while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getc(stdin);}
a=w*s;
}
template<class T,class... Y>void r(T& t,Y&... a){r(t);r(a...);}
char num[200010];
int a[15];
int flag;
int main()
{
int n;
r(n);
gets(num);
for(int i=1;i<=9;i++)
r(a[i]);
for(int i=0;i<n;i++)
{
if(!flag&&num[i]-'0'==a[num[i]-'0'])flag=-1;
if(num[i]-'0'<=a[num[i]-'0'])num[i]=a[num[i]-'0']+'0',flag++;
else if(flag)break;
}
printf("%s",num);
return 0;
}