K-Goodness String-Google Kick Start 2021 Round A

Google Kick Start 2021 Round A:

K-Goodness String

Charles defines the goodness score of a string as the number of indices i such that Si≠SN−i+1 where 1≤i≤N/2 (1-indexed). For example, the string CABABC has a goodness score of 2 since S2≠S5 and S3≠S4. Charles gave Ada a string S of length N, consisting of uppercase letters and asked her to convert it into a string with a goodness score of K. In one operation, Ada can change any character in the string to any uppercase letter. Could you help Ada find the minimum number of operations required to transform the given string into a string with goodness score equal to K?

题目大意

Charles定义一个字符串的“好值”为满足Si ≠ \not= =SN-i+1的下标i的个数(1 ≤ \leq i ≤ \leq N 2 \frac{N}{2} 2N)。现在,给定一个长度为N的字符串,Charles想让你用最少的操作把它变成一个“好值”为K的串,其中每次操作你可以把字符串中的任意一个字符改成任意一个大写字母。求最小操作次数。

思路解析

本轮签到题,直接读入字符串,求出当前“好值”,然后根据与K的大小关系增加或减少相同字符即可。复杂度O(N)。

Solve函数代码如下:

int solve(string s, int k)
{
  int x = 0;
  for(int i = 0; i < s.size() / 2; i++)
  {
     if(s[i] != s[s.size() - i - 1])x++;
  }
  return abs(x-k);
}

本轮所有题目题解(已完结)

K-Goodness String
L-Shaped Plots
Rabbit House
Checksum

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值