关注
暴力全排列。。。总共就6!= 720 种组合,数目不是很大,没超时。 /*
FileName:
Author: Dev_Universe
Date: 2019/9/27 19:34:00
Description:
*/
#include<bits/stdc++.h>
using namespace std;
vector<string> AllKeys;
string temp = "";
char Keys[6] = {'A', 'S', 'D', 'F', 'G', 'H'};
void Perm(char *list, int low, int high)
{
if(high == low)
{
for(int i = 0; i <= low; i++)
{
temp += list[i];
}
AllKeys.push_back(temp);
temp = "";
}
else
{
for(int i = low; i <= high; i++)
{
swap(list[i], list[low]);
Perm(list, low + 1, high);
swap(list[i], list[low]);
}
}
}
int Min_Cost(const string & str, string KeysOrder)
{
map<char, int> Letters;
for(int i = 1; i < 7; i++)
{
Letters[KeysOrder[i-1]] = i;
}
int pos = 1;
char let;
int cost = 0;
for(int i = 0; i < str.length(); i++)
{
let = str[i];
cost += abs(Letters[let] - pos);
pos = Letters[let];
}
return cost;
}
int main()
{
int T;
cin>>T;
map<char, int> Letters;
Perm(Keys, 0, 5);
while(T--)
{
string str;
cin>>str;
int min_cost = 70000;
int cost = 0;
for(int i = 0; i < AllKeys.size(); i++)
{
cost = Min_Cost(str, AllKeys[i]);
if(cost < min_cost)
min_cost = cost;
}
cout<<min_cost<<endl;
}
system("pause");//getchar();
return 0;
}
查看原帖
点赞 1
相关推荐
投递大疆等公司7个岗位 > 大疆工作体验
点赞 评论 收藏
转发
牛客热帖
正在热议
# 牛友的五一计划 #
5265次浏览 160人参与
# 如何看待offer收割机的行为 #
191329次浏览 2961人参与
# 牛客帮帮团来啦!有问必答 #
385909次浏览 7658人参与
# 春招别灰心,我们一人来一句鼓励 #
20261次浏览 301人参与
# 无实习如何秋招上岸 #
170541次浏览 2695人参与
# 如何一边实习一边秋招 #
199342次浏览 3973人参与
# 硬件人的春招flag #
14431次浏览 199人参与
# 晒一晒我的offer #
2805860次浏览 49760人参与
# 非技术岗薪资爆料 #
7111次浏览 135人参与
# 你更愿意参加线上面试还是线下面试? #
6620次浏览 92人参与
# 华为求职进展汇总 #
439533次浏览 4418人参与
# 第一次面试 #
15856次浏览 240人参与
# 在国企工作的人,躺平了吗? #
71976次浏览 872人参与
# 简历中的项目经历要怎么写 #
378908次浏览 6371人参与
# 通信硬件薪资爆料 #
145797次浏览 1095人参与
# 来聊聊机械薪资天花板是哪家 #
21098次浏览 167人参与
# 机械人怎么评价今年的华为 #
54311次浏览 447人参与
# 应届生应该先就业还是先择业 #
12233次浏览 115人参与
# 如果校招重来我最想改变的是 #
71804次浏览 1384人参与
# 租房前辈的忠告 #
20962次浏览 1658人参与