zoukankan      html  css  js  c++  java
  • Codeforces Round #555 (Div. 3) B. Long Number 【仔细读题】

    B. Long Number
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a long decimal number aa consisting of nn digits from 11 to 99. You also have a function ff that maps every digit from 11 to 99 to some (possibly the same) digit from 11 to 99.

    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). For example, if a=1337a=1337, f(1)=1f(1)=1, f(3)=5f(3)=5, f(7)=3f(7)=3, and you choose the segment consisting of three rightmost digits, you get 15531553 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 nn (1n21051≤n≤2⋅105) — the number of digits in aa.

    The second line contains a string of nn characters, denoting the number aa. Each character is a decimal digit from 11 to 99.

    The third line contains exactly 99 integers f(1)f(1), f(2)f(2), ..., f(9)f(9) (1f(i)91≤f(i)≤9).

    Output

    Print the maximum number you can get after applying the operation described in the statement no more than once.

    Examples
    input
    4
    1337
    1 2 5 4 6 6 3 1 9
    
    output
    1557
    
    input
    5
    11111
    9 8 7 6 5 4 3 2 1
    
    output
    99999
    
    input
    2
    33
    1 1 1 1 1 1 1 1 1
    
    output
    33
    题意:在字符串中任意选择一个子串,将该子串中的数字按照题目的替换规则进行替换(无论替换之后,新数字比旧数字大还是小,只要在字串范围之内都要替换),找出替换后的最大数字;
    这个数字替换是在字符串的某一段区间的,在这段区间中所有的就数字都要被新数字替换,所以我们最先找到的子串(其中替换后的新数字都比就数字大,遇到新数字比旧数字小,就停止),就可以的得到最大数字
    错因:一开始以为替换区间是整个字符串,然后按照新数字比就数字大就替换,新数字比就数字小就不替换,然后 WA6了。
     1 #include<stdio.h>
     2 #include<string>
     3 #include<string.h>
     4 using namespace std;
     5 
     6 int n,num[300000],judge[100];
     7 char m[300000];
     8 int main(){
     9     scanf("%d",&n);getchar();
    10     for(int i = 1 ; i <= n ; i++){
    11         scanf("%c",&m[i]);
    12     }
    13     for(int i = 1 ; i <= n ; i++){
    14         num[i] = m[i] - 48;
    15     }
    16     int cnt = n;
    17     for(int i = 1 ; i <= 9 ; i++){
    18         scanf("%d",&judge[i]);
    19     }
    20     int ret = 0;
    21     for(int i = 1 ; i <= n ; i++){
    22         if(ret == 1 && judge[num[i]] < num[i]){
    23             ret++;
    24         }
    25         if(judge[num[i]] > num[i] && ret == 0){
    26             num[i] = judge[num[i]];
    27             ret = 1;
    28         }else if(judge[num[i]] > num[i] && ret == 1){
    29             num[i] = judge[num[i]];
    30         }
    31         
    32         printf("%d",num[i]);
    33     }
    34     printf("
    ");
    35     return 0;
    36 }
    AC代码

     一个从很久以前就开始做的梦。

  • 相关阅读:
    python 网页爬虫,下载网络图片
    J2SE 8的泛型
    简历上的项目经历怎么写?这3条原则不可忽视!
    看过太多大厂面试题,其实考的无非是这 3 点能力
    宁可多花1000元租房,也绝不要去挤半小时地铁
    光背面试题可没用,谈谈如何真正掌握一个技术知识点
    为了学好Java,我尝试了这 6 个方法
    springmvc之处理模型数据SessionAttributes注解
    springmvc之处理模型数据Map
    springmvc之处理模型数据ModelAndView
  • 原文地址:https://www.cnblogs.com/DreamACMer/p/10826806.html
Copyright © 2011-2022 走看看