K-Goodness String-Google Kick Start 2021 Round A

羊舌承
2023-12-01

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

 类似资料: