답안 #803088

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
803088 2023-08-02T21:32:24 Z Augustyn Palindromi (COCI22_palindromi) C++17
0 / 110
1000 ms 174956 KB
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
#define MOD 1000000007
struct pal_tree_node
{
    int dlug,hasz;
    pal_tree_node *shrt[2], *syn[2];
    pal_tree_node(int xd,int hz)
    {
        dlug=xd;
        hasz=hz;
        shrt[0]=shrt[1]=syn[0]=syn[1]=NULL;
    }
};
struct lista
{
    int wart;
    int l, r;
    lista(int a)
    {
        wart=a;
        l=-1;
        r=-1;
    }
    lista()
    {
        wart=0;
        l=-1;
        r=-1;
    }
};

vector<int>nal;
vector<bool>seq;
vector<pal_tree_node*>pref,suf;
pal_tree_node* pust;
pal_tree_node* jz[2];
void ini(int n)
{
    nal.resize(n);
    seq.resize(n);
    pref.resize(n);
    suf.resize(n);
    pust = new pal_tree_node(0,0);
    jz[0] = new pal_tree_node(1,1);
    jz[1] = new pal_tree_node(1,2);
    jz[0]->shrt[0]=jz[1]->shrt[1]=pust;
}
int znajdz(int ter)
{
    if(nal[ter]==ter)
        return ter;
    nal[ter]=znajdz(nal[ter]);
    return nal[ter];
}
int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    cin>>n;
    ini(n);
    vector<lista>akt_ciag(n);
    vector<pair<int,int>>po(n);
    vector<int>wielk(n,1);
    vector<unordered_map<int,bool>>mapy(n);
    vector<vector<int>>tusa(n);
    vector<int>konc(n),gdzie_w_konc(n);
    for(int i=0;i<n;++i)
    {
        char wcz;
        cin>>wcz;
        wcz-='0';
        seq[i]=wcz;
        nal[i]=i;
        pref[i]=suf[i]=jz[wcz];
        akt_ciag[i].l=akt_ciag[i].r=-1;
        akt_ciag[i].wart=i;
        po[i].first=po[i].second=i;
        tusa[i].push_back(wcz+1);
        mapy[i][wcz+1]=1;
    }
    vector<pair<int,int>>zlacz(n-1);
    for(int ile_merge=0;ile_merge<n-1;++ile_merge)
    {
        cin>>zlacz[ile_merge].first>>zlacz[ile_merge].second;
        --zlacz[ile_merge].first; --zlacz[ile_merge].second;
        int a,b;
        a=zlacz[ile_merge].first; b=zlacz[ile_merge].second;
        a=znajdz(a);
        b=znajdz(b);
        akt_ciag[po[a].second].r=po[b].first;
        akt_ciag[po[b].first].l=po[a].second;
        po[a].second=po[b].second;
        nal[b]=a;
    }
    int tut=po[znajdz(0)].first;
    for(int i=0;i<n;++i)
    {
        gdzie_w_konc[tut]=i;
        konc[i]=tut;
        po[tut].first=po[tut].second=i;
        tut=akt_ciag[tut].r;
    }

    for(int i=0;i<n;++i)
    {
        akt_ciag[i].l=akt_ciag[i].r=-1;
        nal[i]=i;
        po[i].first=po[i].second=gdzie_w_konc[i];
    }
    for(int ile_merge=0;ile_merge<n-1;++ile_merge)
    {
        int a,b;
        a=zlacz[ile_merge].first; b=zlacz[ile_merge].second;
        a=znajdz(a);
        b=znajdz(b);
        
        if(1/*wielk[a]<=wielk[b]*/)
        {
            pal_tree_node* najd=pref[b];
            for(int i=po[a].second;i>=po[a].first;--i)
            {
                if(najd->dlug==po[b].second-i)
                {
                    najd=najd->shrt[seq[konc[i]]];
                    if(najd==NULL)
                        najd=pust;
                }
                if(najd->dlug!=0)
                    if(seq[konc[i+1+(najd->dlug)]]!=seq[konc[i]])
                    {
                        najd=najd->shrt[seq[konc[i]]];
                        if(najd==NULL)
                            najd=pust;
                    }
                if(najd->dlug==0)
                {
                    if(seq[konc[i+1]]==seq[konc[i]])
                    {
                        //dwie takie same
                        if(pust->syn[seq[konc[i]]]==NULL)
                        {
                            pal_tree_node* pom= new pal_tree_node(2,(seq[konc[i]]+1)*4);
                            pust->syn[seq[konc[i]]]=pom;
                            pom->shrt[seq[konc[i]]]=jz[seq[konc[i]]];
                        }
                        najd=pust->syn[seq[konc[i]]];
                        if(mapy[b][(seq[konc[i]]+1)*4]!=1)
                        {
                            mapy[b][(seq[konc[i]]+1)*4]=1;
                            tusa[b].push_back((seq[konc[i]]+1)*4);
                        }
                    }
                    else
                    {
                        //jeden taki sam
                        najd=jz[seq[konc[i]]];
                        if(mapy[b][seq[konc[i]]+1]!=1)
                        {
                            mapy[b][seq[konc[i]]+1]=1;
                            tusa[b].push_back(seq[konc[i]]+1);
                        }
                    }
                }
                else
                {
                    //dodajemy normalnie
                    if(najd->syn[seq[konc[i]]]==NULL)
                    {
                        //dodaj wierz
                        pal_tree_node* pom= new pal_tree_node((najd->dlug)+2,(1ll*(najd->hasz)*3+seq[konc[i]])%MOD);
                        najd->syn[seq[konc[i]]]=pom;
                        //znajdz pom->shrt
                        pal_tree_node* pom2=najd->shrt[seq[konc[i]]];
                        if(pom2==NULL)
                            pom2=jz[seq[konc[i]]];
                        else
                            pom2=pom2->syn[seq[konc[i]]];
                        //no i teraz musze wiedziec co jest po nim
                        int x=seq[konc[i+pom2->dlug]];
                        pom->shrt[x]=pom2;
                        pom2=pom2->shrt[1-x];
                        pom->shrt[1-x]=pom2;
                    }
                    najd=najd->syn[seq[konc[i]]];
                    if(mapy[b][najd->hasz]!=1)
                    {
                        mapy[b][najd->hasz]=1;
                        tusa[b].push_back(najd->hasz);
                    }
                }
                //sprawdz czy najd jest sufixem b
                if(najd->dlug==po[b].second-i+1)
                    suf[b]=najd;
            }
            pref[b]=najd;
            po[b].first=po[a].first;
            nal[a]=b;
            for(auto i:tusa[a])
            {
                if(mapy[b][i]!=1)
                {
                    mapy[b][i]=1;
                    tusa[b].push_back(i);
                }
            }
            a=b;
        }
        cout<<tusa[a].size()<<'\n';
    }
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:77:27: warning: array subscript has type 'char' [-Wchar-subscripts]
   77 |         pref[i]=suf[i]=jz[wcz];
      |                           ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1065 ms 174956 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -