Submission #493459

#TimeUsernameProblemLanguageResultExecution timeMemory
493459MasterTasterPalindromes (APIO14_palindrome)C++14
0 / 100
60 ms39460 KiB
#include <bits/stdc++.h>

#define pb push_back
#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define MAXN 600010

using namespace std;

struct PalTree{

    struct Node{
        int len, cnt, to[26], dub, slink;
        Node() { len=0; cnt=0; dub=0; slink=0; for (int i=0; i<26; i++) to[i]=-1; }
        Node(int a, int b, int c, int d)
        {
            len=a; cnt=b; dub=c; slink=d;
            for (int i=0; i<26; i++) to[i]=-1;
        }
    };

    vector<Node> pt;
    int suff;
    int s[MAXN];
    int gde=0;

    void addLetter(int c)
    {
        s[gde]=c;
        while (c!=s[gde-1-pt[suff].len])
            suff=pt[suff].slink;

        int slnd=pt[suff].to[c];
        if (slnd==-1)
        {
            slnd=pt.size();
            pt[suff].to[c]=slnd;
            pt.pb(Node(pt[suff].len+2, 0, 0, 0));
            if (suff==1)
                pt[slnd].slink=2;
            else
            {
                int pom=pt[suff].slink;
                while(c!=s[gde-1-pt[pom].len])
                    pom=pt[pom].slink;
                pt[slnd].slink=pt[pom].to[c];
            }
            pt[slnd].dub=pt[pt[slnd].slink].dub+1;
        }
        pt[slnd].cnt++;
        suff=slnd;
        gde++;
    }

    void calc()
    {
        for (int i=pt.size()-1; i>2; i--){ assert(pt[i].slink>0 && pt[i].slink<pt.size()); pt[pt[i].slink].cnt+=pt[i].cnt; }
    }
    void init()
    {
        pt.clear();
        pt.pb(Node(-1, -1, -1, -1));
        pt.pb(Node(-1, 0, 0, 1));
        pt.pb(Node(0, 0, 0, 1));
        suff=2;
    }

    Node &operator[](int idx) {
        return pt[idx];
    }
};
PalTree pt;
ll ress=-1;
string st;

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

    cin>>st;

    pt.init();
    for (int i=0; i<st.size(); i++) pt.addLetter((int)(st[i]-'a'));
    pt.calc();

    for (int i=3; i<pt.pt.size(); i++)
    {
        ress=max(ress, (ll)(pt[i].cnt*pt[i].len));
    }

    cout<<ress;
}

Compilation message (stderr)

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from palindrome.cpp:1:
palindrome.cpp: In member function 'void PalTree::calc()':
palindrome.cpp:59:79: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<PalTree::Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         for (int i=pt.size()-1; i>2; i--){ assert(pt[i].slink>0 && pt[i].slink<pt.size()); pt[pt[i].slink].cnt+=pt[i].cnt; }
palindrome.cpp: In function 'int main()':
palindrome.cpp:84:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |     for (int i=0; i<st.size(); i++) pt.addLetter((int)(st[i]-'a'));
      |                   ~^~~~~~~~~~
palindrome.cpp:87:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<PalTree::Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |     for (int i=3; i<pt.pt.size(); i++)
      |                   ~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...