Cod sursa(job #2798427)

Utilizator stefanlupoi1Lupoi Stefan stefanlupoi1 Data 11 noiembrie 2021 11:53:33
Problema PScPld Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.38 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("pscpld.in");
//ofstream out("pscpld.out");

const int N = 2e6 + 5;

const int inMax = 1 << 11, outMax = 1 << 11;

class Parser {
private:
    FILE *in, *out;
    int inp, outp;
    char *inBuff, *outBuff;

    char read_ch(){
        if(inp == inMax){
            inp = 0;
            fread(inBuff, 1, inMax, in);
        }
        return inBuff[inp++];
    }
    void writeCh(char ch){
        if(outp == outMax){
            fwrite(outBuff, 1, outMax, out);
            outp = 0;
        }
        outBuff[outp++] = ch;
    }

public:
    Parser(const char* inn, const char* outn){
        in = fopen(inn, "r");
        out = fopen(outn, "w");
        inp = outp = 0;
        inBuff = new char[inMax]();
        outBuff = new char[outMax]();
    }

    ~Parser(){
        fwrite(outBuff, 1, outp, out);
        fclose(out);
        fclose(in);
    }

    Parser& operator >>(int &x){
        char ch;
        while(!isdigit(ch = read_ch()) && ch != '-');
        x = 0;
        int sign = (ch == '-' ? -1 : (x = ch - '0', 1));
        while(isdigit(ch = read_ch())){
            x = x * 10 + ch - '0';
        }
        x *= sign;
        return *this;
    }

    Parser& operator <<(const char* ch){
        while(*ch){
            writeCh(*ch);
            ch++;
        }
        return *this;
    }

    Parser& operator <<(int x){
        if(x < 0) {
            writeCh('-');
            return (*this) << -x;
        }
        if(x <= 9){
            writeCh(x + '0');
        }
        else{
            (*this) << x / 10;
            writeCh(x % 10 + '0');
        }
        return *this;
    }
};

int v[N];
string s1, s2;

int main()
{
    Parser out("pscpld.cazan", "pscpld.out");
    long long result = 0;
    in >> s1;
    s2 = "(*";
    for(char c:s1){
        s2 += c;
        s2 += "*";
    }
    s2 += ")";
    int n = s2.size(), radius = 0, center = 0;
    for(int i = 1; i < n; i++){
        int opposite = 2 * center - i, p = center + radius;
        if(i < p) v[i] = min(p - i, v[opposite]);
        while(s2[i + v[i] + 1] == s2[i - v[i] - 1]) v[i]++;
        if(i + v[i] > p)
            center = i;
        radius = v[i];
    }
    for(int i = 1; i < n; i++){
        result += (v[i] + 1) / 2;
    }
    out << result;
    return 0;
}