7-7 选民投票 STL

7-7 选民投票

编程统计候选人的得票数。有若干位候选人(n<=10),候选人姓名从键盘输入(候选人姓名不区分大小写,姓名最长为9个字节),若干位选民,选民每次输入一个得票的候选人的名字(姓名最长为9个字节),若选民输错候选人姓名,则按废票处理。程序自动统计各候选人的得票结果,并按照得票数由高到低的顺序排序。最后输出各选票人得票结果和废票信息。
输入格式:

先输入候选人人数n和n名候选人姓名,再输入选民人数m和m位选民的选票。
输出格式:

先根据选票结果由高到低输出各候选人得票结果,再根据废票情况输出废票信息(换行后,输出提示信息“invalid vote:”,再输出废票信息)。
输入样例1:

3
zhang
li
wang
9
Wang
Zhang
zhuang
LI
Liao
ZHANG
WANG
Wang
wang

输出样例1:

wang:4
zhang:2
li:1

invalid vote:
zhuang
Liao

输入样例2:

2
liu
yang
5
Liu
liu
YANG
yang
Liu

输出样例2:

liu:3
yang:2

用STL瞎模拟就行了

#ifdef debug
#include <time.h>
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

	char print_f[105];
	void read() {}
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T, typename... T2>
		inline void print(T x, T2... oth) {
			ll p3=-1;
			if(x<0) putchar('-'), x=-x;
			do{
				print_f[++p3] = x%10 + 48;
			} while(x/=10);
			while(p3>=0) putchar(print_f[p3--]);
			putchar(' ');
			print(oth...);
		}
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K;

std::map<string, int> mp;
std::map<string, string> mp2;
//std::set<string> fp;
std::vector<string> fp;

#define CH(x) ((x>='a' && x<='z') ? x : x-'A'+'a')

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	cin >> n;
	string str;
	while(n--) {
		cin >> str;
		string tmp = str;

		for(int i=0; i<(int)tmp.length(); i++)
			tmp[i] = CH(tmp[i]); //全部转小写

		mp[tmp] = 0;
		mp2[tmp] = str;
	}
	cin >> m;
	while(m--) {
		cin >> str;
		string tmp = str;
		
		for(int i=0; i<(int)tmp.length(); i++)
			tmp[i] = CH(tmp[i]);

		if(mp.find(tmp) == mp.end()) 
			fp.push_back(str);
		else 
			mp[tmp] ++;
	}
	vector<pair<int, string> > vec;
	for(auto it : mp)
		vec.push_back({ it.second, mp2[it.first]} );
	sort(vec.begin(), vec.end(), greater<pair<int, string> >());
	for(auto it : vec) 
		cout << it.second << ":" << it.first<< endl;
	if(fp.size())
		cout << "\ninvalid vote:" << endl;
	for(auto s : fp) cout << s << endl;





#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}




全部评论

相关推荐

比亚迪 求帮选offer 12k*1.36*12 双非硕
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务