题解 | 小红的字符串修改

小红的字符串修改

https://www.nowcoder.com/practice/66e0054ff6b345afa47bcd4e8ceb72d7

/*
1.认真读题还是需要训练的
2.检查的时候最好手动模拟一遍吧
3.
*/

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    string s;cin>>s;string t;cin>>t;
    int res = 100000000;
    // 计算字符相减的绝对值相加之和
    int n = t.length(),m = s.length();
    for (int i = 0;i < n-m+1;i++)
    {
        int temp = 0;
        for (int j = 0;j < m;j++)
        {
            temp+=min(abs(t[i+j]-s[j]),26-abs(t[i+j]-s[j]));
        }
        res = min(temp,res);
    }
    cout<<res<<'\n';
}
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);solve();return 0;}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务