在一行上输入一个长度为
且为偶数,仅由小写字母构成的字符串
,代表待修改的字符串。
输出一个整数,表示将
修改为双生串的最小修改次数。
popipa
3
在这个样例中,将
修改为
是其中一个最优解。
aaaa
0
在这个样例中,给定的字符串已经是双生串,不需要修改。
java实现,注意题目描述是前半部分都一样,后半部分都一样,左右两部分长度必须相等!用left和right数组分别统计左半部分的各元素出现次数和右半部分各元素出现次数,为了让左半部分都是一个字母,需要修改次数为左半部分的长度 - 左半部分统计的最大次数。同理,右半部分也是一样,最后把两者相加即可得到结果7.19
import java.util.Scanner;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String s = in.nextLine();
int mid = s.length() / 2 - 1;
int[] left = new int[26];
int[] right = new int[26];
for (int i = 0; i <= mid; i++){
left[s.charAt(i) - 'a']++;
right[s.charAt(i + mid + 1) - 'a']++;
}
Arrays.sort(left);
Arrays.sort(right);
System.out.println((mid + 1 - left[25]) + (mid + 1 - right[25]));
}
}
} import java.util.*;
public class Main {
// 本质上就是用前半段/后半段的总长度减去出现次数最多的字符的数量
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.nextLine();
String first = input.substring(0, input.length() / 2);
String last = input.substring(input.length() / 2);
Map<Character, Integer> firstMap = getMap(first);
Map<Character, Integer> lastMap = getMap(last);
int times = getCount(firstMap) + getCount(lastMap);
System.out.print(times);
}
private static Map<Character, Integer> getMap(String str) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
return map;
}
private static int getCount(Map<Character, Integer> map) {
if (map.size() == 1) { // 代表只有一种字母,不需要修改
return 0;
}
int maxValue = 0; // 获取到出现次数最多的字符串
int length = 0;
for (char key : map.keySet()) {
int value = map.get(key);
length += value;
if (value > maxValue) {
maxValue = value;
}
}
// 用总长度减去最多的字符串数量
return length - maxValue;
}
} from collections import Counter st = input() mid = len(st)//2 st1 = st[:mid] st2 = st[mid:] st1_list = sorted(Counter(st1).items(), key=lambda d: d[1], reverse=True) st2_list = sorted(Counter(st2).items(), key=lambda d: d[1], reverse=True) c = len(st)-st1_list[0][1]-st2_list[0][1] print(c)
#include <stdio.h>
#include <string.h>
int main() {
char str[300000];
int i, j, len, lMax=0, rMax=0, sum;
int lChar[26] = {0}, rChar[26] = {0};
scanf("%s", str);
len = (int)strlen(str);
if(len == 1) {
printf("1");
}
for(i = 0, j = len/2; i < len/2 && j<len; i++, j++) {
lChar[str[i]-'a']++;
rChar[str[j]-'a']++;
lMax = lMax > lChar[str[i]-'a'] ? lMax : lChar[str[i]-'a'];
rMax = rMax > rChar[str[j]-'a'] ? rMax : rChar[str[j]-'a'];
}
sum = (len/2 -lMax) + (len/2 - rMax);
printf("%d\n", sum);
return 0;
} s = input().strip()
if not (1<=len(s)<=2*10**5 and len(s)%2==0 and all(si.isalpha() and si.islower() for si in s)):
print('请输入有效长度内且为偶数,仅由小写字母构成的字符串s')
else:
shalf = len(s)//2
fores = s[:shalf]
backs = s[shalf:]
forec = {}
backc = {}
for si in fores:
forec[si] = forec.get(si, 0) + 1
num = shalf - max(forec.values())
for si in backs:
backc[si] = backc.get(si, 0) + 1
num += shalf - max(backc.values())
print(num)
s = input() n = int(len(s)/2) s1 = s[:n] s2 = s[n:] x = set(s1) y = set(s2) max1= max([s1.count(i) for i in x]) max2= max([s2.count(j) for j in y]) p = (n-max1) + (n-max2) print(p)
package main
import (
"fmt"
)
func main() {
var s string
fmt.Scan(&s)
n := len(s)
s1 := s[:n/2]
s2 := s[n/2:]
set1 := make(map[rune]int)
set2 := make(map[rune]int)
for _, c := range s1 {
set1[c]++
}
for _, c := range s2 {
set2[c]++
}
max1 := maxValueInMap(set1)
max2 := maxValueInMap(set2)
fmt.Println(n-max1-max2)
}
func maxValueInMap(m map[rune]int) int {
max := 0
for _, v := range m {
if v > max {
max = v
}
}
return max
} #include <iostream>
#include <unordered_map>
using namespace std;
int main() {
string str;
cin >> str;
unordered_map<char, int> urd_map_1;
for ( int i = 0; i < str.length() / 2; i++ ) {
urd_map_1[ str[i] ]++;
}
int res1 = ( *urd_map_1.begin() ).second;
for ( auto map : urd_map_1 ) {
if ( map.second > res1 ) {
res1 = map.second;
}
}
res1 = str.length() / 2 - res1;
unordered_map<char, int> urd_map_2;
for ( int i = str.length() / 2; i < str.length(); i++ ) {
urd_map_2[ str[i] ]++;
}
int res2 = ( *urd_map_2.begin() ).second;
for ( auto map : urd_map_2 ) {
if ( map.second > res2 ) {
res2 = map.second;
}
}
res2 = str.length() / 2 - res2;
cout << res1 + res2 << endl;
}
// 64 位输出请用 printf("%lld")