韩跳跳


私信TA

用户名:hyn123456

访问量:895

签 名:

等  级
排  名 34235
经  验 403
参赛次数 0
文章发表 2
年  龄 0
在职情况 学生
学  校 陕西师范大学
专  业

  自我简介:

TA的其他文章

青蛙跳杯子
浏览:364

官方数据错误,不完整的循环节也输出了答案,例如 abcde 当 k 取 3 的时候,可以修改为 abc,这是不符合题意的。

如果非要过官方数据,修改代码 -1 输出的条件为 length < k,且将字符串长度截断为abc

以下解题先不考虑错误数据

解题思路:

1、首先判断能否将字符串整分,即判断s.length() 能否整除k,可以的话,再继续往下走,否则返回答案-1

2、既然可以整分,那就手动分开吧,创建一个二维数组,该数组每行k个元素,共s.length()/k行,其实也可以不使用二维数组,直接对字符串进行操作,但使用二维数组更直观一些。
3、统计每中各字符出现的个数,然后把字符都修改成出现频率最高的那个字符,并统计修改次数(这里我是用HashMap来统计的)

例如:2    aabbaa

                a                      a
                b                      b
                a                      a
第一列中,a出现的次数最多,需要把所有字符都改为a,需要改动一次

第二列中,a出现的次数最多,需要把所有字符都改为a,需要改动一次

共改动2次,输出2即可

参考代码:

注意:这个代码通不过官方的检测,但实际是对的,因为官方数据有错误!下边也会给出能通过官方检测的代码

import java.util.*;

public class Main {
   public static int result;

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       int k = scan.nextInt();
       String s = scan.next();
       if (s.length() % k != 0) {
           result = -1;
       } else {
           int len = s.length() / k;
           char[][] target = new char[k][len];
           for (int i = 0; i < k; i++) {
               for (int j = 0; j < len; j++) {
                   target[i][j] = s.charAt(i * len + j);
               }
           }
           for (int i = 0; i < len; i++) {
               //统计每列中字符出现的次数
               Map<Character, Integer> statistics = new HashMap<Character, Integer>();
               for (int j = 0; j < k; j++) {
                   char ch = target[j][i];
                   if (statistics.containsKey(ch)) {
                       int count = statistics.get(ch);
                       count = count + 1;
                       statistics.put(ch, count);
                   } else {
                       statistics.put(ch, 1);
                   }
               }
               //计算将将该列中的字符都改为该列出现次数最多的字符需要改动几次,并添加到result中
               int max = 0;
               for (Character ch : statistics.keySet()) {
                   int temp = statistics.get(ch);
                   if (temp > max) {
                       max = temp;
                   }
               }
               result += k - max;
           }
       }
       System.out.print(result);
       scan.close();
   }
}

修改后的代码:

import java.util.*;

public class Main {
    public static int result;
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int k=scan.nextInt();
        String s=scan.next();
        if(s.length()<k){
            result=-1;
        }else{
            int length=s.length()-s.length()%k;
            int len=length/k;
            char[][] target=new char[k][len];
            for (int i = 0; i < k; i++) {
                for (int j = 0; j < len; j++) {
                    target[i][j]=s.charAt(i*len+j);
                }
            }
            for (int i = 0; i < len; i++) {
                //统计每列中字符出现的次数
                Map<Character,Integer> statistics= new HashMap<Character, Integer>();
                for (int j = 0; j < k; j++) {
                    char ch=target[j][i];
                    if(statistics.containsKey(ch)){
                        int count=statistics.get(ch);
                        count=count+1;
                        statistics.put(ch,count);
                    }else{
                        statistics.put(ch,1);
                    }
                }
                //计算将将该列中的字符都改为该列出现次数最多的字符需要改动几次,并添加到result中
                int max=0;
                for (Character ch: statistics.keySet() ) {
                    int temp=statistics.get(ch);
                    if(temp>max){
                        max=temp;
                    }
                }
                result+=k-max;
            }
        }
        System.out.print(result);
        scan.close();
    }
}


 

0.0分

11 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

你好,第二个代码最后为什么要k-max
2022-03-28 09:44:42
你好,请问一下,是哪个数据不通过呢?第一个代码
2022-03-09 19:59:17
  • «
  • 1
  • »