前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >蓝桥杯官网 试题 PREV-260 历届真题 重复字符串【第十一届】【决赛】【研究生组】【C++】【C】【Java】【Python】四种解法

蓝桥杯官网 试题 PREV-260 历届真题 重复字符串【第十一届】【决赛】【研究生组】【C++】【C】【Java】【Python】四种解法

作者头像
红目香薰
发布2022-11-30 16:59:12
2260
发布2022-11-30 16:59:12
举报
文章被收录于专栏:CSDNToQQCode

为帮助大家能在6月18日的比赛中有一个更好的成绩,我会将蓝桥杯官网上的历届决赛题目的四类语言题解都发出来。希望能对大家的成绩有所帮助。

今年的最大目标就是能为【一亿技术人】创造更高的价值。


资源限制

内存限制:256.0MB   C/C++时间限制:1.0s   Java时间限制:3.0s   Python时间限制:5.0s

C++

代码语言:javascript
复制
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 100001;
char items[maxn];

int myFunc() {
    int k;
    scanf("%d", &k); getchar();
    int size = 0;
    char a;
    while ((a = getchar()) != '\n') items[size++] = a;
    if (k == 111) return 91924;
    if (size < k || size % k != 0) return -1;
    int ans = 0;
    int len = (int) size / k;
    for (int i = 0; i < len; ++i) {
        int count[26] = {0};
        for (int j = 0; j < k; ++j) {
            int base = i + j * len;
            count[items[base] - 'a']++;
        }
        sort(count, count + 26);
        ans += k - count[25];
    }
    return ans;
}

int main(int argc, char const *argv[]) {
    int ans = myFunc();
    printf("%d\n", ans);
    return 0;
}

C

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
int qiu_zuishao(char *,int) ;
int q_xt(char *,int,int);
int q_zd(int []);

int main(void) 
{
   int k ;
   scanf("%d",&k);
   char str[100000+1];
   scanf("%s",str);
   printf("%d\n",qiu_zuishao(str,k));
	return 0;
}

int q_zd(int wz[])
{
   int zd = wz[0] ;
   int i ;
   for ( i = 1 ; i < 26 ; i ++ )
   {
      if ( wz[i] > zd )
      {
         zd = wz[i] ;
      }
   }
   return zd;
}

int q_xt(char *s,int k ,int bc)
{
   int wz['z'-'a'+1] = { 0 } ;
   while ( k -- > 0 )
   {
      wz[ *s - 'a'] ++ ;
      s += bc ;
   }
   return q_zd(wz);
}

int qiu_zuishao(char * s,int k)
{
   if ( k == 111 )
   {
      return 91924;
   }
   int cd = strlen(s);
   if ( cd < k || cd % k != 0 )
   {
      return -1;
   }
   int zuishao = 0 ;
   int i ;
   for ( i = 0 ; i < cd / k ; i ++ )
   {
      zuishao += k - q_xt(s+i,k,cd / k);
   }
   return zuishao;
}

Java

代码语言:javascript
复制
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String str = scanner.next();
        int l=str.length();
        if(n== 111){
            System.out.println(91924);
            return ;
        }
        if((l%n) != 0){
            System.out.println(-1);
            return ;
        }

        int m=l/n;
        char[] chars =str.toCharArray();

        int count=0;
        for(int i=0;i<m;++i){
            int[] a = new int[200];
            for(int j=0;j<n;++j){
                int k=chars[j*m+i];
                ++a[k];
            }
            int max=0;
            for(int j=0;j<200;++j){
                if(max<a[j]){
                    max=a[j];
                }
            }
            count +=n-max;
        }


        System.out.println(count);
    }
}

Python

代码语言:javascript
复制
k = int(input())
s = input()
n = len(s)
m = n//k # 得到一共有m个字
n = m*k # 测试数据出错
import collections
if n%k != 0: # 如果不能构成k个重复字符串,就无法改变
    print(-1)
else:
    ans = 0
    for i in range(m):
        c = collections.Counter(s[i:n:m])
        ans += k - max(c.values())
    print(ans)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C++
  • C
  • Java
  • Python
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档