Cod sursa(job #3209440)

Utilizator maftenoMaftei Alex-Cristian mafteno Data 2 martie 2024 13:18:27
Problema Regiuni Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <bits/stdc++.h>
#define P 123457
#define Q 777013
using namespace std;

ifstream fin("parpal.in");
ofstream fout("parpal.out");

string s;
int n;

bool Rez()
{
    int i, b, p1, p2, x1, x2, x3, x4, l;
    n = s.length();
    b = 26; p1 = p2 = 1;
    l = 0;
    x1 = x2 = x3 = x4 = 0;
    for (i = 0; i < n; i++)
    {
        x1 = (x1 * b + s[i] - 'a') % P;
        x3 = (x3 * b + s[i] - 'a') % Q;
        x2 = ((s[i] - 'a') * p1 + x2) % P;
        x4 = ((s[i] - 'a') * p2 + x4) % Q;
        p1 = p1 * b % P;
        p2 = p2 * b % Q;
        l++;
        if (x1 == x2 && x3 == x4 && l % 2 == 0)
        {
             x1 = x2 = x3 = x4 = 0;
             p1 = p2 = 1;
             l = 0;
        }
    }
    if (p1 == 1)
        return "DA\n";
    return "NU\n";
}

int main()
{
    int t;
    fin >> t;
    while (t--)
    {
        fin >> s;
        fout << Rez();
    }
    return 0;
}