Cod sursa(job #2776686)

Utilizator UnknownPercentageBuca Mihnea-Vicentiu UnknownPercentage Data 20 septembrie 2021 18:28:30
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.21 kb
#include <bits/stdc++.h>

using namespace std;

inline void Open(const string Name) {
    #ifndef ONLINE_JUDGE
        (void)!freopen((Name + ".in").c_str(), "r", stdin);
        (void)!freopen((Name + ".out").c_str(), "w", stdout);
    #endif
}

char s[1001];
char c;

int a[27], after[27], inexp[27], poz[1001];
int ok, len;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    Open("a");

    for(int i = 1;i <= 26;i++) 
        a[i] = i;

    cin.getline(s, sizeof(s));
    for(int i = 0;s[i];i++)
        if(s[i] != ' ') s[len++] = s[i];
    s[len] = '\0';

    for(int i = 0;i < len;i++)
        poz[i] = INT_MAX;

    stack <int> st;
    for(int i = 0;s[i];i++) {
        if(s[i] != ' ' && s[i] == s[i + 1]) {
            if(i + 2 < len && s[i + 2] >= 'a' && s[i + 2] <= 'z') {
                c = s[i + 2];
                if(s[i] == '-') poz[i + 2] = --a[c - 'a' + 1];
                if(s[i] == '+') poz[i + 2] = ++a[c - 'a' + 1];
                s[i] = s[i + 1] = ' ';
            }

            if(i - 1 >= 0 && s[i - 1] >= 'a' && s[i - 1] <= 'z') {
                c = s[i - 1];
                if(s[i] == '-') after[c - 'a' + 1]--;
                if(s[i] == '+') after[c - 'a' + 1]++;
                s[i] = s[i + 1] = ' ';
            }
            continue;
        }

        if(s[i] >= 'a' && s[i] <= 'z' && poz[i] == INT_MAX) {
            //cout << s[i] << " ";
            poz[i] = a[s[i] - 'a' + 1]; 
        }
    }

    len = 0;
    for(int i = 0;s[i];i++) {
        if(s[i] != ' ') {
            s[len] = s[i], poz[len] = poz[i];
            len++;
        }
    }
    s[len] = '\0';

    // cout << s << "\n";
    // for(int i = 0;i < len;i++)
    //     cout << poz[i] << " ";

    for(int i = 0;s[i];i++) {
        if(s[i] >= 'a' && s[i] <= 'z') {
            inexp[s[i] - 'a' + 1] = 1;
            if(i - 1 >= 0 && s[i - 1] == '-') st.push(-poz[i]);
            else st.push(poz[i]);
        }
    }

    int sum = 0;
    while(!st.empty()) {
        sum += st.top();
        st.pop();
    }

    cout << sum << "\n";
    for(int i = 1;i <= 26;i++)
        if(inexp[i]) cout << a[i] + after[i] << "\n";
    
    return 0;
}