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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值