小红定义一个字符串是双生串,当且仅当其前半部分所有字符相同,后半部分所有字符相同。 现在,小红拿到了一个字符串 ,她每次操作可以修改一个字符。小红希望你求出将其修改为双生串的最小修改次数。
区块链毕设网qklbishe.com为您提供问题的解答
小红定义一个字符串是双生串,当且仅当其前半部分所有字符相同,后半部分所有字符相同。
现在,小红拿到了一个字符串
,她每次操作可以修改一个字符。小红希望你求出将其修改为双生串的最小修改次数。
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here while ((line = await readline())) { const pirot = Math.floor(line.length / 2); const s1 = line.substring(0, pirot); const s2 = line.substring(pirot); const hash1 = new Map(), hash2 = new Map(); let max1 = 1, max2 = 1; for (let i = 0; i < s1.length; i++) { if (hash1.has(s1[i])) { hash1.set(s1[i], hash1.get(s1[i]) + 1); } else { hash1.set(s1[i], 1); } max1 = Math.max(max1, hash1.get(s1[i])); } for (let i = 0; i < s2.length; i++) { if (hash2.has(s2[i])) { hash2.set(s2[i], hash2.get(s2[i]) + 1); } else { hash2.set(s2[i], 1); } max2 = Math.max(max2, hash2.get(s2[i])); } console.log(line.length - max1 - max2) } })();
50:46
EASY
#include <algorithm> #include <iostream> #include <unordered_map> using namespace std; int main() { string str; cin >> str; unordered_map<char, int> first_map; unordered_map<char, int> second_map; int half = str.size() / 2; for (int i = 0; i < half; ++i) ++first_map[str[i]]; for (int i = half; i < str.size(); ++i) ++second_map[str[i]]; char first_max = max_element(first_map.begin(), first_map.end(), [](const auto & a, const auto & b) { return a.second < b.second; })->first; char second_max = max_element(second_map.begin(), second_map.end(), [](const auto & a, const auto & b) { return a.second < b.second; })->first; int cnt = 0; for (int i = 0; i < half; ++i) { if (str[i] != first_max) ++cnt; } for (int i = half; i < str.size(); ++i) { if (str[i] != second_max) ++cnt; } cout << cnt; }
39:46
以上就是关于问题小红定义一个字符串是双生串,当且仅当其前半部分所有字符相同,后半部分所有字符相同。
现在,小红拿到了一个字符串 ,她每次操作可以修改一个字符。小红希望你求出将其修改为双生串的最小修改次数。的答案
欢迎关注区块链毕设网-
web3一级市场套利打新赚钱空投教程
区块链NFT链游项目方科学家脚本开发培训