题意:英语是硬伤。错了好屡次,多错了题意。能够从数字串的某一个位置开始替换一段字串f(xi),不能超过一次。输出答案。git
You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in aa, and replace each digit xx from this segment with f(x)f(x).this
思路:从头开始找,找到第一个须要替换的,从那个位置开始替换。记住后面找的时候加上f[s[i]-'0']>=s[i]-'0'。spa
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int f[10]; int main(){ int n,a; string s; cin>>n>>s; f[0]=0; for(int i=1;i<=9;i++) cin>>f[i]; for(int i=0;i<n;i++){ if(f[s[i]-'0']>s[i]-'0'){ int j=i; while(f[s[j]-'0']>=s[j]-'0'){ s[j]=f[s[j]-'0']+'0'; j++; } break; } } cout<<s<<endl; return 0; }