沈阳哪个男科医院好,什么叫做seo,淘宝网页版消息在哪里,嘉兴网站建设多少钱题目链接#xff1a;登录—专业IT笔试面试备考平台_牛客网
题目#xff1a;
样例#xff1a; 输入 yab 输出 3 思路#xff1a; 暴力枚举#xff0c;全部变成对应的26个字母字符需要的操作步数#xff0c;取最少的一个操作步数#xff0c;
这里的操作步数#xff0…题目链接登录—专业IT笔试面试备考平台_牛客网
题目
样例
输入 yab
输出 3 思路 暴力枚举全部变成对应的26个字母字符需要的操作步数取最少的一个操作步数
这里的操作步数是 环形 26 字母数组中 两个字符之间最短距离。
环形数组的两个元素之间的最短距离关系是
环形数组两个元素之间的最短距离关系
i j 分别表示两个不同的元素int dis min(abs(i - j),环形数组大小 - abs(i - j))这里环形数组的大小是 26 。
代码详解如下
#include iostream
#include cstring
#include algorithm
#include queue
#include unordered_map
#define endl \n
#define x first
#define y second
#define mk make_pair
#define int long long
#define NO puts(NO)
#define YES puts(YES)
#define umap unordered_map
#define INF 0x3f3f3f3f3f3f3f3f
#define All(x) (x).begin(),(x).end()
#pragma GCC optimize(3,Ofast,inline)
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N 2e6 10;
using PII pairint,int;
inline void solve()
{string s;int ans INF; // ans 取正无穷方便取 mincin s;// 遍历 26 个字母for(int i 0;i 26;i){int tem 0; // 临时存储总距离// 全部字符变成 对应的字母for(char j : s){int c j - a; // 对应字母int mid abs(i - c); // 中间的距离int out 26 - abs(i - c); // 全部距离 - 中间距离 外边距离int dis min(mid,out);tem dis;}ans min(ans,tem); // 取最少总距离即最少操作数}cout ans endl;
}
signed main()
{
// freopen(a.txt, r, stdin);___G;int _t 1;
// cin _t;while (_t--){solve();}return 0;
}
最后提交