暴力全排列。。。总共就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

相关推荐

投递美团等公司10个岗位
点赞 评论 收藏
转发
牛客网
牛客企业服务