package 米哈游; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; //A3bc2X-b2 public class 压缩字符串 { static HashMap<Character,Integer> map = new HashMap<>(); static HashMap<Character,Integer> tempMap = new HashMap<>(); static int max = 0; static char maxCh ='z'+1; public static void main(String[] args) { Scanner sc = new Scanner(System.in); String line = sc.nextLine(); Character pre = null,next = null; for(int i = 0 ; i < line.length() ; i++) { char ch = line.charAt(i); //如果不是字母 if(ch<'A'||ch>'z') { //下划线 if(ch=='-') { pre = line.charAt(i-1); next = line.charAt(++i); // System.out.println("pre"+pre); // System.out.println("pre"+next); for(int j = pre ; j <=next ; j++) { if(j>'Z'&&j<'a') continue; int val = 0; if(tempMap.containsKey((char)j)) val = tempMap.get((char)j); tempMap.put((char)j, val+1); //System.out.println((char)j+"---"+(val+1)); } } //数字 else { int num = Integer.parseInt(ch+"");//倍数 cal(num); //重置缓存 tempMap = new HashMap<>(); } } //字母 else { int val = 0; if(tempMap.containsKey(ch)) val = tempMap.get(ch); tempMap.put(ch, val+1); pre = ch; } } cal(1); System.out.println(""+maxCh+max); } public static void cal(int num) { Iterator<Character> chs = tempMap.keySet().iterator(); //遍历缓存,合并到map中 while(chs.hasNext()) { int val = 0,newVal = 0; char tempCh = chs.next();  if(map.containsKey(tempCh)) val = map.get(tempCh); newVal = val+tempMap.get(tempCh)*num; //System.out.println(tempCh+":"+tempMap.get(tempCh)+":"+num+":"+newVal); if(newVal==0) newVal++; map.put(tempCh, newVal); if(tempCh<'a') { int v = 0; char BtempCh = (char)(tempCh+('a'-'A')); //System.out.println("BtempCh:"+BtempCh); if(map.containsKey(BtempCh)) v = map.get(BtempCh); newVal += v; //System.out.println(map.get(BtempCh)); tempCh = BtempCh; //System.out.println(v); }else { int v = 0; char StempCh = (char)(tempCh-('a'-'A')); //System.out.println("StempCh:"+StempCh); if(map.containsKey(StempCh)) v = map.get(StempCh); newVal += v; //System.out.println(map.get(StempCh)); //tempCh = StempCh; //System.out.println(v); } //更新最大值 if(newVal>=max) { if(tempCh<maxCh) { max = newVal; maxCh = tempCh; } } } } } 我太难了,一道都没写出来,这道题还是结束后20分钟才调出来的,,,,,好惨啊
点赞 评论

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务