[筆試練習]今日頭條實習筆試
阿新 • • 發佈:2019-02-19
我天,第一題做了好久還是85%的通過率。。改日再戰。。
#include <iostream> #include <string> #include <vector> #include <map> #include <unordered_set> #include <algorithm> using namespace std; int main() { int n; while (cin >> n) { vector<string> record; string temp; unordered_set<char> first; unordered_set<char> all; map<unsigned long long, vector<char>> m; vector<unsigned long long> v(26, 0); for (int i = 0; i<n; i++) { cin >> temp; first.insert(temp[0]); record.push_back(temp); int len = temp.size(); unsigned long long flag = 1; for (int j = len - 1; j >= 0; j--) { v[temp[j] - 'A'] += flag; all.insert(temp[j]); flag *= 10; } } for (int i = 0; i<10; i++) { m[v[i]].push_back('A' + i); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); map<char, unsigned long long> m2; int countdown = 10; for (int i = 0; i < 10; i++) { int len = m[v[i]].size(); for (int j = 0; j < len; j++) { m2[m[v[i]][j]] = --countdown; cout << m[v[i]][j] << ' ' << m2[m[v[i]][j]] << endl; } if (len>0) { i += len - 1; } } map<char, unsigned long long>::iterator ite=m2.end(); map<char, unsigned long long>::iterator ite2; ite--; char last = ite->first; if (ite->second==0&&first.find(last) != first.end()) { ite2=ite; ite2--; while (first.find(ite2->first) != first.end()) { ite2--; } int temp = ite->second; ite->second = ite2->second; ite2->second = temp; cout << ite->first << ' ' << ite->second << endl; cout << ite2->first << ' ' << ite2->second << endl; } unsigned long long ret = 0; for (int i = 0; i < n; i++) { string temp = record[i]; int len = temp.size(); unsigned long long cur = 0; for (int j = 0; j < len; j++) { cur = cur * 10 + m2[temp[j]]; } ret += cur; } cout << ret << endl; } return 0; }