ALDS1_3_D 2018-2-21

#include
#include
#include
#include
#include
#include
using namespace std;

int main() {
    stack S1;
    stack >S2;
    char s[20000 + 10];
    scanf("%s", s);
    int i = 0, sum = 0;
    while (s[i] != '\0') {
        if (s[i] == '\\')
            S1.push(i);
        else if (s[i] == '/' && !S1.empty()) {
            int j = S1.top(); S1.pop();
            sum += i - j;
            int a = i - j;

            while (!S2.empty() && S2.top().first > j) {
                a += S2.top().second; S2.pop();
            }
            S2.push(make_pair(j, a));
        }
        i++;
    }

    vector ans;
    while (!S2.empty()) {
        ans.push_back(S2.top().second);
        S2.pop();
    }
    printf("%d\n", sum);
    int len = ans.size();
    printf("%d", len);
    for (int i = len - 1; i >= 0; i--) {
        printf(" %d", ans[i]);
    }
    printf("\n");
    return 0;
}

你可能感兴趣的:(ACM动态规划)