第二周训练 :F

F - Long Number

You are given a long decimal number a consisting of n digits from 1 to 9. You also have a function f that maps every digit from 1 to 9 to some (possibly the same) digit from 1 to 9.

You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in a, and replace each digit x from this segment with f(x). For example, if a=1337, f(1)=1, f(3)=5, f(7)=3, and you choose the segment consisting of three rightmost digits, you get 1553 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 n (1≤n≤2⋅105) — the number of digits in a.

The second line contains a string of n characters, denoting the number a. Each character is a decimal digit from 1 to 9.

The third line contains exactly 9 integers f(1), f(2), …, f(9) (1≤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

题目坑点:
1.如果从头开始遍历,在第一次数字减小的地方截止的话,会导致有一种特殊情况错误(从头开始都是数字不变的,然后出现一个变小的数字,此时选择的区间内没有可增大的数字),解决措施:从第一个数字增大的数字开始遍历(变一下起点位置)
2.所选择的是一个连续区间,且保证数字最大(即,数字越靠前,变大时数字越大)
3.区间内数字需增大或不变(不变也符合条件,可以继续向后扩大区间)

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class F {
	//改变后的数字1~9
	static int[] help = new int[]{1,2,3,4,5,6,7,8,9};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		String str = sc.nextLine();
		StringBuilder sb = new StringBuilder(str);//字符串转StringBuilder
		boolean[] ch = new boolean[9];
		int[] arr = new int[9];
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < arr.length; i++) {
			arr[i] = sc.nextInt();
			if(arr[i] >= help[i]) {
				if(arr[i] > help[i]) list.add(i+1);
				ch[i] = true;
				help[i] = arr[i];
			}
		}
		int min = 100;
		for (int i = 0; i < list.size(); i++) {
			int temp = str.indexOf(list.get(i)+"");
			if(temp >= 0 && min > temp) {
				min = temp;
			}
		}
		if(min == 100) min = 0;
		for (int i = min; i < n; i++) {
			int num = check(i,str);
			if(ch[num-1] == true) {
				sb.setCharAt(i, change(num));
			}else {
				break;
			}
		}
		System.out.println(sb);
		sc.close();
	}
	private static char change(int num) {
		int temp = help[num-1];
		switch(temp){
			case 0 : return '0';
			case 1: return '1';
			case 2: return '2';
			case 3: return '3';
			case 4: return '4';
			case 5: return '5';
			case 6 : return '6';
			case 7: return '7';
			case 8: return '8';
			case 9: return '9';
		}
		return '0';
	}
	private static int check(int i, String str) {
		char c = str.charAt(i);
		switch(c){
			case '0' : return 0;
			case '1': return 1;
			case '2': return 2;
			case '3': return 3;
			case '4': return 4;
			case '5': return 5;
			case '6' : return 6;
			case '7': return 7;
			case '8': return 8;
			case '9': return 9;
		}
		return 0;
	}
}

代码可以简化,将check方法和change方法改变
‘0’(字符0)的ASCII码是48
‘1’ - ‘0’ = 1,可以使用这种方法进行变换
同理:1+‘0’='1’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值