CF: Long Number

                                                题目链接


#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n;
    cin >> n;
    string a;
    cin >> a;
    int f[10];
    for(int i = 1; i <= 9; i++)
        cin >> f[i];
    for(int i = 0; i < n; i++)
    {
        if(f[a[i] - '0'] > (a[i] - '0'))
        {
            int j = i;
            while(j < n && f[a[j] - '0'] >= (a[j] - '0'))  /*这里要注意j的边界问题,至于为什么 >= 见分析1*/
            {
                a[j] = f[a[j] - '0'] + '0';
                j++;
            }
            break;

        }
    }
    cout << a << endl;
}

分析1:题目原文中有这么一句“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).”, 谷歌翻译后结果为  “您可以执行以下操作不超过一次:在a中选择一个非空的连续子数字段,并用f(x)替换该段中的每个数字x。” 那么为啥写成上面的形式就不难理解了,替换时只能是比它本身大或者和它本身相等,若比它小就不可以了,另外注意判断的顺序是从前往后。

posted @ 2019-05-17 00:30  DIY-Z  阅读(148)  评论(0编辑  收藏  举报