codechef Popular Rice Recipe题解

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题解

时间: 2024-11-09 01:45:18

codechef Popular Rice Recipe题解的相关文章

codechef Ciel and Receipt题解

Tomya is a girl. She loves Chef Ciel very much. Tomya like a positive integer p, and now she wants to get a receipt of Ciel's restaurant whose total price is exactly p. The current menus of Ciel's restaurant are shown the following table. Name of Men

Codechef Maximum Weight Difference题解

Maximum Weight Difference Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants hi

codechef - Bytelandian gold coins 题解

In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to ma

codechef The Lead Game 题解

The game of billiards involves two players knocking 3 balls around on a green baize table. Well, there is more to it, but for our purposes this is sufficient. The game consists of several rounds and in each round both players obtain a score, based on

codechef The Morning Commute 题解

The Morning Commute The Chef commutes to work every day using the city's underground metro. The schedule for the trains has recently been changed and he wants to know how long it will take to travel from the station nearest to his house and the stati

codechef Jewels and Stones 题解

Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery accessories. She has been collecting stones since her childhood - now she has become really good with identifying which ones are fake and which ones are no

codechef Three Way Communications 题解

The Chef likes to stay in touch with his staff. So, the Chef, the head server, and the sous-chef all carry two-way transceivers so they can stay in constant contact. Of course, these transceivers have a limited range so if two are too far apart, they

Codechef Not a Triangle题解

找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,但是时间效率就是O(n*n*n)了,很慢. 不过既然是组合问题就必定可以使用排序后处理的方法降低时间效率的. 这里降低时间效率的方法是: 选一个最大的数c,然后选两个小数a和b,其中a < b,如果符合条件,那么两个数中间的数必定都可以作为b符合条件a + b < c 这样可以把时间效率降到O(n*n). 这个规律也不好找啊.要

codechef Subtraction Game 1题解

Subtraction Game 1 Chef is playing a game on a sequence of N positive integers, say A1, A2, ... AN. The game is played as follows. If all the numbers are equal, the game ends. Otherwise Select two numbers which are unequal Subtract the smaller number