码迷,mamicode.com
首页 > 其他好文 > 详细

codechef Popular Rice Recipe题解

时间:2014-05-07 04:05:46      阅读:428      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   tar   

Popular Rice Recipe

Did you know that there are over 40,000 varieties of Rice in the world ? There are so many dishes that can be prepared with Rice too. A famous chef from Mumbai, Tid Gusto prepared a new dish and named it ‘Tid Rice‘. He posted the recipe in his newly designed blog for community voting, where a user can plus (+) or minus (-) the recipe. The final score is just the sum of all votes, where (+) and (-) are treated as +1 and -1 respectively. But, being just a chef ( and not a codechef ) he forgot to take care of multiple votes by the same user.

A user might have voted multiple times and Tid is worried that the final score shown is not the correct one. Luckily, he found the user logs, which had all the N votes in the order they arrived. Remember that, if a user votes more than once, the user‘s previous vote is first nullified before the latest vote is counted ( see explanation for more clarity ). Given these records in order ( and being a codechef yourself :) ), calculate the correct final score.

Input

First line contains T ( number of testcases, around 20 ). T cases follow. Each test case starts with N ( total number of votes, 1 <= N <= 100 ). Each of the next N lines is of the form "userid vote" ( quotes for clarity only ), where userid is a non-empty string of lower-case alphabets ( ‘a‘ - ‘z‘ ) not more than 20 in length and vote is either a + or - . See the sample cases below, for more clarity.

Output

For each test case, output the correct final score in a new line

Example

Input:
3
4
tilak +
tilak +
tilak -
tilak +
3
ratna +
shashi -
ratna -
3
bhavani -
bhavani +
bhavani -

Output:
1
-2
-1

模拟统计论坛投票。

使用map容器,很方便解决。

#pragma once
#include <cstdio>
#include <string>
#include <unordered_map>
#include <iostream>

class PopularRiceRecipe
{
	std::unordered_map<std::string, bool> umSi;
public:
	void run()
	{
		int T;
		scanf("%d", &T);
		while (T--)
		{
			int n;
			scanf("%d", &n);
			umSi.clear();
			std::string str;
			char a;
			while (n--)
			{
				std::cin>>str;
				//std::cin>>a;
				scanf("\n%c\n", &a);
				if (‘-‘ == a) umSi[str] = false;
				else umSi[str] = true;//only count the last vote
			}
			int votes = 0;
			for (auto x:umSi)
			{
				if (x.second) votes++;
				else votes--;
			}
			printf("%d\n", votes);
		}
	}
};

int popularRiceRecipeRun()
{
	PopularRiceRecipe prr;
	prr.run();
	return 0;
}




codechef Popular Rice Recipe题解,布布扣,bubuko.com

codechef Popular Rice Recipe题解

标签:des   style   blog   class   code   tar   

原文地址:http://blog.csdn.net/kenden23/article/details/25041209

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!