Codeforces Beta Round #5

A - Chat Server’s Outgoing Traffic

Polycarp is working on a new project called “Polychat”. Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types of commands:

Include a person to the chat (‘Add’ command).
Remove a person from the chat (‘Remove’ command).
Send a message from a person to all people, who are currently in the chat, including the one, who sends the message (‘Send’ command).
Now Polycarp wants to find out the amount of outgoing traffic that the server will produce while processing a particular set of commands.

Polycarp knows that chat server sends no traffic for ‘Add’ and ‘Remove’ commands. When ‘Send’ command is processed, server sends l bytes to each participant of the chat, where l is the length of the message.

As Polycarp has no time, he is asking for your help in solving this problem.

Input

Input file will contain not more than 100 commands, each in its own line. No line will exceed 100 characters. Formats of the commands will be the following:

+<name> for ‘Add’ command.
-<name> for ‘Remove’ command.
<sender_name>:<message_text> for ‘Send’ command.
<name> and <sender_name> is a non-empty sequence of Latin letters and digits. <message_text> can contain letters, digits and spaces, but can’t start or end with a space. <message_text> can be an empty line.

It is guaranteed, that input data are correct, i.e. there will be no ‘Add’ command if person with such a name is already in the chat, there will be no ‘Remove’ command if there is no person with such a name in the chat etc.

All names are case-sensitive.

Output

Print a single number — answer to the problem.

Examples

Input

+Mike
Mike:hello
+Kate
+Dmitry
-Dmitry
Kate:hi
-Kate

Output

9

Input

+Mike
-Mike
+Mike
Mike:Hi   I am here
-Mike
+Kate
-Kate

Output

14

#include <cstdio>
#include <cstring>
using namespace std;

char s[105];

int main(void)
{
	int ans = 0, cnt = 0;
	while (gets(s))
	{
		if (s[0] == '+')
			cnt++;
		else if (s[0] == '-')
			cnt--;
		else
		{
			int i, len = strlen(s);
			for (i = 0; i < len; i++)
			{
				if (s[i] == ':')
				{
					i++;
					break;
				}
			}
			ans += cnt * (len - i);
		}
	}
	printf("%d\n", ans);
	return 0;
}

B - Center Alignment

Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor «Textpad» decided to introduce this functionality into the fourth release of the product.

You are to implement the alignment in the shortest possible time. Good luck!

Input

The input file consists of one or more lines, each of the lines contains Latin letters, digits and/or spaces. The lines cannot start or end with a space. It is guaranteed that at least one of the lines has positive length. The length of each line and the total amount of the lines do not exceed 1000.

Output

Format the given text, aligning it center. Frame the whole text with characters «*» of the minimum size. If a line cannot be aligned perfectly (for example, the line has even length, while the width of the block is uneven), you should place such lines rounding down the distance to the left or to the right edge and bringing them closer left or right alternatively (you should start with bringing left). Study the sample tests carefully to understand the output format better.

Examples

Input

This  is

Codeforces
Beta
Round
5

Output

************
* This  is *
*          *
*Codeforces*
*   Beta   *
*  Round   *
*     5    *
************

Input

welcome to the
Codeforces
Beta
Round 5

and
good luck

Output

****************
*welcome to the*
*  Codeforces  *
*     Beta     *
*   Round 5    *
*              *
*      and     *
*  good luck   *
****************

文章排版問題,讓文字居中顯示。如果兩邊不對稱時,一個靠左顯示,下一個不對稱的靠右顯示。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char str[1005][1005];
int len[1005];

int main(void)
{
	bool flag = 0;
	int ma = 0, i = 0;//ma表示每行最大的字節數,i表示有多少行 
	while (gets(str[i]))
	{
		len[i] = strlen(str[i]);
		ma = max(ma, len[i]);
		i++;
	}
	for (int j = 0; j < ma + 2; j++)
		printf("*");
	printf("\n");
	for (int j = 0; j < i ; j++)//第幾行 
	{
		printf("*");
		for (int h = 0; h < (ma - len[j] + flag) / 2; h++)//輸出內容左邊的空格
			printf(" ");
		for (int h = 0; h < len[j]; h++)//輸出內容
			printf("%c", str[j][h]);
		for (int h = (ma - len[j] + flag) / 2 + len[j]; h < ma; h++)//輸出內容右邊的空格
			printf(" ");
		printf("*\n");
		if ((ma - len[j]) % 2 != 0)
			flag = !flag;
	}
	for (int j = 0; j < ma + 2; j++)
		printf("*");
	printf("\n");
	return 0;
}

C - Longest Regular Bracket Sequence

This is yet another problem dealing with regular bracket sequences.

We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.

You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.

Input

The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 10^6.

Output

Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing “0 1”.

Examples

Input

)((())))(()())

Output

6 2

Input

))(

Output

0 1

以下是改編的楊dalao的代碼。
棧內記錄的是每個括號的下標,如果遍歷到左半部分就進棧,遍歷到右半部分且棧頂是左半部分就出棧。
下面的代碼可以解決只有一種括號的情況,還可以解決多種括號且平衡的情況。但是如果遇到多組括號不平衡的話,就會返回錯誤的結果。這個時候就需要用到區間dp。

#include <iostream>
#include <stack>
#include <cstring>
#include <string>
using namespace std;
const int N = 1e6 + 5;

string s;
stack<int> st;

int main(void)
{
	cin >> s;
    int ans = 0, num = 0;//最大長度,出現的次數 
    int len = s.size();
    for (int i = 0; i < len; i++)
	{
        if (s[i] == ')' && st.size() && s[st.top()] == '(')//能配對 
			st.pop();
        else 
			st.push(i);
        if(st.size())
		{
        	if(ans == i - st.top())
        		num++;
        	else if (ans < i - st.top())
        		num = 1;
        	ans = max(ans, i - st.top());
        }
        else
		{
        	if (ans == i + 1)
        		num++;
        	else if (ans < i + 1)
        		num = 1;
        	ans = max(ans, i + 1);
        } 
    }
    if (ans == 0)
    	num = 1;
    cout << ans << " " << num << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章