Word memory method

Link: https://ac.nowcoder.com/acm/contest/11334/C
Source: Niuke

Topic description
Niuniu has finished the 4th and 6th grades and is ready to share his English learning methods.

Niuniu: The most important thing to learn English is to memorize words. If you can memorize all the words, then your English can become the best in the world.

However, Niuniu’s memory method is to convert each letter of a word into a number, consider A as 1, B as 2, C as 3{}A as 1, B as 2, C as 3, and so on , And then calculate the sum of each letter of the word. From then on, every time you think of this word, you must first think of the sum of the word, and then try to figure out the sum.

Soon after, Niuniu updated his memory method again, and can merge repeated consecutive letters.

For example, write ABCABC as (ABC)2, HHHH as (H2)2 or H4{}ABCABC as (ABC)2, and HHHH as (H2)2 or H4, so that when calculating the sum, you only need to multiply the number with the sum inside. Okay, it's more convenient. (But sometimes Niuniu did not find that several identical consecutive letters are repeated due to presbyopia, so he did not merge)

Now it’s time for Niuniu to test you. Niuniu tells you a word. This word may be so long that you have never seen it before, but Niuniu wants you to calculate the sum of this word according to his method.

Input description: There
is only one line of input, which means that the word S{} that Niuniu gave you is only one line, which means that the word S
|S|<=10^5∣S∣<=10
5

S contains only uppercase letters, left and right brackets and numbers, but no other characters {}S contains only uppercase letters, left and right brackets and numbers, but no other characters

Output description:
output one line, indicating the sum of this word {} output one line, indicating the sum of this word.
Ensure that the sum is not greater than 10^{14} Ensure that the sum is not greater than 10
14

Example 1
Input
Copy
(A2B2) 2
Output
Copy
12
Remarks: The
parentheses may be nested,
or the number after the letter may be more than one digit, or there may be no number. If there is no number, it means that it appears once, but this number is not May be 0

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
#define LL long long
int pos[N],st[N],top,n;
char ch[N];
LL solve(int l,int r) {
    
    
    LL tot = 0;
//    cout << l << " " << r << endl;
    for(int i = l;i <= r;) {
    
    
        if(pos[i]) {
    
    
            LL res = solve(i + 1,pos[i] - 1);
            i = pos[i] + 1;
            LL x = 0;
            while(i <= r && isdigit(ch[i]))
            x = x * 10 + ch[i] - '0',i++;
            if(x) tot += res * x;
            else tot += res;
        }
        else {
    
    
            if(i + 1 <= r && isdigit(ch[i + 1]))
            {
    
    
                LL x = 0,old = i;i = i + 1;
                while(i <= r && isdigit(ch[i])) 
                x = x * 10 + ch[i] - '0',i++;
                tot +=  x * (ch[old] - 'A' + 1);
            }
            else tot += ch[i] - 'A' + 1,i++;
        }
    }
    return tot;
}
int main() {
    
    
    scanf("%s",ch+1);
    n = strlen(ch+1);
    for(int i = 1;i <= n;i++) {
    
    
        if(ch[i] == '(') st[++top] = i;
        if(ch[i] == ')') {
    
    pos[st[top]] = i;top--;}
    }
    cout << solve(1,n) << endl;
}

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/112563307