Cod sursa(job #2788610)

Utilizator GrizzyProblemSolver79cNot telling GrizzyProblemSolver79c Data 26 octombrie 2021 05:37:17
Problema PScPld Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <bits/stdc++.h>

using namespace std;

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

int const NMAX = 1000000;
int n;
string input;
string s;
int dp[1 + 2 * NMAX + 1];

int main() {
  fin >> input;
  s.push_back('$');
  s.push_back(input[0]);
  for(int i = 1; i < input.length(); i++) {
    s.push_back('*');
    s.push_back(input[i]);
  }
  s.push_back('#');
  //cout << s << "\n";
  n = s.length();
  dp[0] = 0;
  int c = 0;
  int right = c;
  //cout << 0 << " " << dp[0] << " " << c << " " << right << "\n";
  for(int i = 1; i < n; i++) {
    if(i < right) {
      dp[i] = min(dp[2*c-i], n-i-1);
    }

    while(s[i-dp[i]-1] == s[i+dp[i]+1]) {
      dp[i]++;
    }

    if(i + dp[i] > right) {
      c = i;
      right = i + dp[i];
    }
    //cout << i << " " << dp[i] << " " << c << " " << right << "\n";
  }
  int center = 0;
  int radius = 0;
  for(int i = 0; i < n; i++) {
    if(dp[i] > radius) {
      radius = dp[i];
      center = i;
    }
  }
  string ans = "";
  for(int i = center - radius; i <= center + radius; i++) {
    if(s[i] != '#' && s[i] != '*' && s[i] != '$') {
      ans.push_back(s[i]);
    }
  }
  fout << ans;
}