...
<Saika> I want to get some water from this strange lake. I have a bottle.
<Keeper> OK.
<Saika> Then I want to go forward to look into the parterre.
<Keeper> More details?
<Saika> Err..What will happen if I let some water drip on the flowers?
<Keeper> ...Err...The flowers will all become super-huge monsters and it will be very dangerous.
<Keeper> Ready to fight.
<Saika> 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:
<notation> ::= <term> "+" <notation> | <term> "-" <notation> | <term> <term> ::= <factor> "*" <term> | <factor> "/" <term> | <factor> <factor> ::= "(" <notation> ")" | <integer> | <dice> <integer> ::= <digit> <integer> | <digit> <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" <dice> ::= <integer> "d" <integer> | "d" <integer>
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:
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.
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.
For each test case, output the format string.
3 d6+1 ((2d6) +5)*((12* 3d6)) 2d10 * d100
[d6] + 1 = [Result] ((([d6] + [d6])) + 5) * ((12 * ([d6] + [d6] + [d6]))) = [Result] ([d10] + [d10]) * [d100] = [Result]
#include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #include<set> #include<map> #include<vector> #include<queue> #include<bitset> #include<algorithm> #include<time.h> #include<assert.h> using namespace std; void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } #define MS(x,y) memset(x,y,sizeof(x)) #define MC(x,y) memcpy(x,y,sizeof(x)) #define MP(x,y) make_pair(x,y) #define ls o<<1 #define rs o<<1|1 typedef long long LL; typedef unsigned long long UL; typedef unsigned int UI; template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; } template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; } const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f; int casenum, casei; char a[N]; char b[N]; char c[N]; int num[N]; int st[N]; int ed[N]; bool sig[128]; int main() { sig['+'] = sig['-'] = sig['*'] = sig['/'] = 1; scanf("%d", &casenum); getchar(); for (casei = 1; casei <= casenum; ++casei) { //第一步,把原串的不可见字符删除 gets_s(a); int n = 0; for (int i = 0; a[i]; ++i)if (!isspace(a[i]))b[n++] = a[i]; b[n] = 0; //第二步,把原串的d'扩展 for (int i = 0; i < n; ++i)num[i] = 0; for (int i = 0; i < n; ++i) { if (b[i] == 'd') { int p = i; while (p&&isdigit(b[p - 1]))--p; if (p == i)num[p] = 1; else for (int j = p; j < i; ++j)num[p] = num[p] * 10 + b[j] - '0',assert(num[p] > 1); st[p] = i; ed[p] = i + 1; while (ed[p] < n - 1 && isdigit(b[ed[p] + 1]))++ed[p]; } } //第三步,完全扩展 int m = 0; for (int i = 0; i < n; ++i) { if (num[i]) { if (num[i] > 1)c[m++] = '('; for (int tim = 1; tim <= num[i]; ++tim) { c[m++] = '['; for (int j = st[i]; j <= ed[i]; ++j)c[m++] = b[j]; c[m++] = ']'; if (tim < num[i]) { c[m++] = ' '; c[m++] = '+'; c[m++] = ' '; } } if(num[i] > 1)c[m++] = ')'; i = ed[i]; } else c[m++] = b[i]; if (sig[b[i]] || sig[b[i + 1]])c[m++] = ' '; }c[m] = 0; printf("%s", c); printf(" = [Result]\n"); } return 0; } /* 【trick&&吐槽】 1,先去空格再处理字符串是个好办法 2,把含有'd'的数字段头尾标记然后整体处理是个好办法 【题意】 给你若干个字符串。 让你把字符串转换成一定的格式。要求如下—— 1,<notation> ::= <term> "+" <notation> | <term> "-" <notation> | <term> <term> ::= <factor> "*" <term> | <factor> "/" <term> | <factor> <factor> ::= "(" <notation> ")" | <integer> | <dice> <integer> ::= <digit> <integer> | <digit> <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" <dice> ::= <integer> "d" <integer> | "d" <integer> 2,Expand dice notations. The <dice> field like "3d5" should be expanded to "([d5] + [d5] + [d5])". If only one dice is rolled in this field, simply replaced it with "[dX]". 3,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. 4,End with specific content. Add " = [Result]" to the end of the format string. 【类型】 模拟 【分析】 这道题是比较简单的模拟。注意几个细节—— 1,我们要得到'd'前的数字为多少。如果这个数字>1,我们需要扩展,需要外加( ) 如果这个数字==1,我们需要忽略1 如果这个数字==0,我们需要丢弃 2,我们需要忽略的是空白字符,"Tab"('\t') and "Space"都是需要被我们丢弃的 【时间复杂度&&优化】 O(n) */