답안 #803105

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
803105 2023-08-02T21:58:01 Z Augustyn Palindromi (COCI22_palindromi) C++17
30 / 110
1000 ms 180108 KB
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
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,500000001);
    jz[1] = new pal_tree_node(1,500000002);
    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,(najd->hasz)*3+seq[konc[i]]+1);
                        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:76:27: warning: array subscript has type 'char' [-Wchar-subscripts]
   76 |         pref[i]=suf[i]=jz[wcz];
      |                           ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 316 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 8 ms 3200 KB Output is correct
16 Correct 3 ms 1236 KB Output is correct
17 Correct 6 ms 2260 KB Output is correct
18 Correct 6 ms 1620 KB Output is correct
19 Correct 30 ms 10920 KB Output is correct
20 Correct 11 ms 3564 KB Output is correct
21 Correct 58 ms 20384 KB Output is correct
22 Correct 23 ms 6356 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 2 ms 724 KB Output is correct
26 Correct 1 ms 712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1090 ms 180108 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 316 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 8 ms 3200 KB Output is correct
16 Correct 3 ms 1236 KB Output is correct
17 Correct 6 ms 2260 KB Output is correct
18 Correct 6 ms 1620 KB Output is correct
19 Correct 30 ms 10920 KB Output is correct
20 Correct 11 ms 3564 KB Output is correct
21 Correct 58 ms 20384 KB Output is correct
22 Correct 23 ms 6356 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 2 ms 724 KB Output is correct
26 Correct 1 ms 712 KB Output is correct
27 Execution timed out 1090 ms 180108 KB Time limit exceeded
28 Halted 0 ms 0 KB -