Блог пользователя ericdai1

Автор ericdai1, история, 4 года назад, По-английски

Hi I attempted this problem in today's contest and I am very confused about this problem and what I am doing wrong. Firstly, my code is in Java (only language i'm good enough at to attempt these tough problems), and I decided to use HashMaps to store each SINGLE letter as well as each DOUBLE letter (ex: "ab", "ba", "cp", etc.) and got their total values at the end to see which one was in the string the most. That way, I knew how many letters I could keep and just subtracted that from the total string length to find the minimum number of deletions. However, it keeps giving the wrong answer for Test Case 2, and I don't know how to make my code any better. https://codeforces.net/contest/1389/problem/C


import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int max = 0; HashMap<String, Integer> map = new HashMap<String, Integer>(); String cyc = sc.next(); for (int j = 0; j < cyc.length(); j++) { String single = cyc.substring(j, j+1); if (j < cyc.length()-1 && !single.equals(cyc.substring(j+1,j+2))) { String doub = cyc.substring(j, j+2); if (map.containsKey(doub)) { map.replace(doub, map.get(doub)+1); max = Math.max(max, map.get(doub) *2); } else { map.put(doub, 1); max = Math.max(max, map.get(doub) *2); } } if (map.containsKey(single)) { map.replace(single, map.get(single)+1); max = Math.max(max, map.get(single)); } else { map.put(single, 1); max = Math.max(max, map.get(single)); } } int fin = cyc.length() - max; System.out.println(fin); } } }
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
4 года назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

Check the following Java solution. It should give you a hint about how to compute the correct answer.

88418302

Note that by definition, there are two possible cases for composing a good string:

  1. A string that consists of the same digit, such as 1111..1.
  2. A string that consists of repeated pattern of 2-digit substring, xy, where x is not equal to y, such as 1212...12.