[笔试练习]今日头条实习笔试

我天,第一题做了好久还是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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值