字符覆盖
字符覆盖
http://www.nowcoder.com/practice/d7ae059c1cee491989412c4fa39d4384
import java.util.Arrays;
import java.util.Scanner;
class CoverRes {
private String s;
private int pos;
public CoverRes() { } public CoverRes(String s, int pos) { this.s = s; this.pos = pos; } public String getS() { return s; } public void setS(String s) { this.s = s; } public int getPos() { return pos; } public void setPos(int pos) { this.pos = pos; }
}
/**
字符覆盖
@Author zhuyq
@Date 2021-09-16
/
public class Main {public static void main(String[] args) {
Scanner in = new Scanner(System.in); String s = in.nextLine(); String t = in.nextLine(); System.out.println(g(s, t));
}
//字符串t覆盖字符串s得到最大字典序 public static String g(String s, String t){ char[] chars = t.toCharArray(); Arrays.sort(chars); int count = 0; String t1 = ""; for(int i=chars.length-1; i>-1; i--){ t1+=chars[i]; count++; } t = t1; for(int j=0; j<t.length(); j++){ char c = t.charAt(j); CoverRes coverRes= f(s, c); if(coverRes.getPos()==s.length()){ return coverRes.getS(); } else{ s = coverRes.getS(); } } return s; } //字符串t中的一个字符覆盖字符串得到最大字典序 public static CoverRes f(String s, char c){ int pos = 0; boolean isAvailable = false; for(int i=0; i<s.length(); i++){ int ascii = (int)s.charAt(i); if(ascii<(int)c){ isAvailable=true; s = s.substring(0, i)+c+s.substring(i+1, s.length()); pos = i; break; } } if(!isAvailable){ pos = s.length(); } return new CoverRes(s, pos); }
}