题解 | 小红的字符串修改
小红的字符串修改
https://www.nowcoder.com/practice/66e0054ff6b345afa47bcd4e8ceb72d7
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main() {
std::string fatherstring;
std::string substring;
cin >> substring >> fatherstring;
//win1
int lengthsubstr = substring.size();
int lengthdiff = fatherstring.size() - lengthsubstr;
std::set<int> exchangetimes;
for (int i = 0 ;i<lengthdiff;i++)
{
int sumtimes = 0;
for (int s = 0; s < lengthsubstr; s++)
{
sumtimes += 26 - abs(substring[s] - fatherstring[i + s]) > abs(substring[s] - fatherstring[i + s]) ? abs(substring[s] - fatherstring[i + s]) : 26 - abs(substring[s] - fatherstring[i + s]);
}
exchangetimes.insert(sumtimes);
}
cout << *exchangetimes.begin()<< endl;
return 0;
}
