您的位置:首页 > 其它

ZOJ-3930-Dice Notation【模拟】

2016-04-11 17:45 351 查看

ZOJ-3930-Dice Notation



I want to get some water from this strange lake. I have a bottle.

OK.

Then I want to go forward to look into the parterre.

More details?

Err..What will happen if I let some water drip on the flowers?

…Err…The flowers will all become super-huge monsters and it will be very dangerous.

Ready to fight.

WHAT THE HELL???



A tabletop role-playing game, or pen-and-paper role-playing game, or table-talk role-playing game is a form of role-playing game (RPG) in which the participants describe their characters’ actions through speech. Participants determine the actions of their characters based on their characterization, and the actions will succeed or fail according to a formal system of rules and guidelines. Within the rules, players have the freedom to improvise. Their choices shape the direction and outcome of the game.

The outcomes of some actions are determined by the rules of the game. For example, while looking around the room, a character may or may not notice an important object or secret doorway, depending on the character’s powers of perception. This usually involves rolling dice, and comparing the number rolled to their character’s statistics to see whether the action was successful. Typically, the higher the character’s score in a particular attribute, the higher their probability of success. Combat is resolved in a similar manner, depending on the character’s combat skills and physical attributes.

Dice notation (also known as dice algebra, common dice notation, RPG dice notation, and several other titles) is a system to represent different combinations of dice in role-playing games using simple algebra-like notation such as “2d6 + 12”.

In most role-playing games, dice rolls required by the system are given in the form of “NdX”. N and X are variables, separated by the letter “d”, which stands for dice. N is the number of dice to be rolled (usually omitted if 1), and X is the number of faces of each dice. For example, if a game would call for a roll of “d4” or “1d4”, this would mean roll a 4-sided dice. While “3d6” would mean roll three six-sided dices. An X-sided dice can get an integer between 1 and X with equal probability.

To this basic notation, an additive modifier can be appended, yielding expressions of the form of “NdX + B”. Here, B is a number to be added to the sum of the rolls. We can use a minus sign (“-“) to indicate subtraction. So, “1d20 - 10” would indicate a roll of a single 20-sided dice with 10 being subtracted from the result. Further more, we can use multiplication (““) or division (“/”) to do some more compilcated calculations like “(2d6 + 5) 10 / (12 - 3d6)”.

To be specific, here is a standard BNF describes the dice notation:

::= “+”

| “-”

|

::= “*”

| “/”

|

::= “(” “)”

|

|

::=

|

::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9”

::= “d”

| “d”

To have a clearer result of a dice notation in a game, our poor player, Saika, decides to write a program as a dice bot. To standardize the output information, the program needs to generate a format string from user’s input string. It should:

Expand dice notations. The field like “3d5” should be expanded to “([d5] + [d5] + [d5])”. If only one dice is rolled in this field, simply replaced it with “[dX]”.

Trim whitespaces. There should be one and only one space character existed around operators (“+” / “-” / “*” / “/”). No extra whitespaces characters (including “Tab” and “Space”) are allowed in the format string.

End with specific content. Add ” = [Result]” to the end of the format string.

However, Saika is fighting against some indescribable monsters now. She has no time to write this program by herself. Please help her to finish it.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There is a line contains a valid dice notation. The length of the notation won’t exceed 2000.

Output

For each test case, output the format string.

Sample Input

3

d6+1

((2d6) +5)((12 3d6))

2d10 * d100

Sample Output

[d6] + 1 = [Result]

((([d6] + [d6])) + 5) * ((12 * ([d6] + [d6] + [d6]))) = [Result]

([d10] + [d10]) * [d100] = [Result]

题目链接:ZOJ-3930

题目大意:看样例就能懂,比较水的模拟题,可惜比赛的时候没时间去写。

有用的部分就是最后三条规则,总结起来就是,对于每行输入的字符串:

形如3d6的输入替换成([d6] + [d6] + [d6]),将1d6或d6换成[d6]

去掉所有空格或Tab,然后在‘+’‘-’‘*’‘/’这四个符号两边各加一个空格

在字符串末尾加上‘[result]’

另外,有一个很玄学的事情。 string += string2 会爆内存,改成string = string + string2就不会了

以下是代码:

//
//  ZOJ-3930-Dice Notation.cpp
//  ZOJ
//
//  Created by pro on 16/4/11.
//  Copyright (c) 2016年 pro. All rights reserved.
//

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
vector <string> ans;
long long change_num(string ret)
{
long long tmp = 0;
for (int i = 0; i <ret.size() ; i++)
{
tmp = tmp * 10 + (ret[i] - '0');
}
return tmp;
}
int main()
{
int t;
cin >> t;
getchar();
while(t--)
{
string s;
getline(cin,s);
ans.clear();
string tmp;
int isdig = 0,isalp = 0; // 判断前面是否出现过数字
int ret = 1;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '(')
{
ans.push_back("(");
}
else if (isdigit(s[i]))
{
tmp = tmp + s[i];
isdig = 1;
}
else if (isalpha(s[i]))
{
isalp = 1;
if (isdig)
{
ret = change_num(tmp);
}
tmp = "";
}
else
{
if (s[i] == ' ' || s[i] == '\t') continue;
string ut;
if (isalp)
{
if(ret > 1)ans.push_back("(");
string sut = "";
ut = "[";
ut = ut + "d";
ut = ut + tmp;
ut = ut + "]";
for (int i = 1; i <= ret; i++)
{
sut = sut + ut;
//     ans.push_back(ut);
//     if (i != ret) ans.push_back("+");
if (i != ret) sut = sut + " + ";
}
ans.push_back(sut);
if (ret > 1)ans.push_back(")");
}
else if (tmp != "")
{
ans.push_back(tmp);
}
ut = s[i];
ans.push_back(ut);
tmp = "";
isdig = 0;
isalp = 0;
ret = 1;
}
}
string ut;
if (isalp)
{
if(ret > 1)ans.push_back("(");
ut = "[";
ut = ut + "d";
ut = ut + tmp;
ut = ut + "]";
string sut = "";
for (int i = 1; i <= ret; i++)
{
sut = sut + ut;
if (i != ret) sut = sut + " + ";
//   ans.push_back(ut);
//   if (i != ret) ans.push_back("+");
}
ans.push_back(sut);
if (ret > 1)ans.push_back(")");
}
else if (tmp != "")
{
ans.push_back(tmp);
}
ans.push_back("=");
for (int i = 0; i < ans.size(); i++)
{
cout << ans[i];
if (ans[i] == "(") continue;
if (i < ans.size() - 1)
{
if (ans[i + 1] == ")") continue;
}
cout << " ";
}
cout << "[Result]\n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: