답안 #223338

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
223338 2020-04-15T07:27:34 Z cheeheng Lampice (COCI19_lampice) C++14
17 / 110
5000 ms 5112 KB
#include <bits/stdc++.h>
using namespace std;

char S[50005];

vector<int> AdjList[50005];

const long long MOD = (1LL<<55)-3;
const int MOD2 = 80000001;

long long pow26[50005];

long long dist[50005];
long long dist2[50005];
int dist3[50005];

int main(){
    int N;
    scanf("%d", &N);

    assert(MOD%13 != 0);
    assert(MOD2%13 != 0);

    pow26[0] = 1;
    for(int i = 1; i <= N; i ++){
        pow26[i] = (pow26[i-1]*26)%MOD;
    }

    for(int i = 1; i <= N; i ++){
        scanf(" %c", &S[i]);
    }

    bool subtask2 = true;
    for(int i = 1; i < N; i ++){
        int a, b;
        scanf("%d%d", &a, &b);
        AdjList[a].push_back(b);
        AdjList[b].push_back(a);
        if(a == i && b == i+1 || b == i && a == i+1){
        }else{
            subtask2 = false;
        }
    }

    if(subtask2){
        int ans = 0;
        for(int i = 1; i <= N; i ++){
            int forward1 = 0;
            int backward1 = 0;

            for(int j = i; j <= N; j ++){
                forward1 = (forward1*26+(S[j]-'0'))%MOD2;

                backward1 += pow26[j-i]*(S[j]-'0');
                backward1 %= MOD;

                if(forward1 == backward1){
                    ans = max(ans, j-i+1);
                }
            }
        }
        printf("%d", ans);
        return 0;
    }

    if(N <= 50000){
        int ans = 0;
        for(int i = 1; i <= N; i ++){
            memset(dist, -1, sizeof(dist));
            queue<int> q;
            q.push(i);
            dist[i] = S[i]-'a';
            dist2[i] = S[i]-'a';
            dist3[i] = 0;
            while(!q.empty()){
                int u = q.front(); q.pop();
                for(int v: AdjList[u]){
                    if(dist[v] == -1){
                        dist[v] = (26*dist[u] + (S[v]-'a'))%MOD;
                        dist3[v] = dist3[u]+1;
                        dist2[v] = (pow26[dist3[u]+1]*(S[v]-'a') + dist2[u])%MOD;
                        q.push(v);
                    }
                }
            }

            for(int j = i; j <= N; j ++){
                //printf("%d %d %lld %lld\n", i, j, dist[j], dist2[j]);
                assert(dist[j] != -1);
                if(dist[j] == dist2[j]){
                    ans = max(ans, 1+dist3[j]);
                }
            }
        }

        printf("%d", ans);
        return 0;
    }

    return 0;
}

Compilation message

lampice.cpp: In function 'int main()':
lampice.cpp:39:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
         if(a == i && b == i+1 || b == i && a == i+1){
            ~~~~~~~^~~~~~~~~~~
lampice.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
lampice.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %c", &S[i]);
         ~~~~~^~~~~~~~~~~~~~
lampice.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 1920 KB Output is correct
2 Correct 32 ms 1920 KB Output is correct
3 Correct 87 ms 2048 KB Output is correct
4 Correct 180 ms 2088 KB Output is correct
5 Correct 5 ms 1536 KB Output is correct
6 Correct 5 ms 1536 KB Output is correct
7 Correct 5 ms 1536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5072 ms 3704 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5038 ms 5112 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 1920 KB Output is correct
2 Correct 32 ms 1920 KB Output is correct
3 Correct 87 ms 2048 KB Output is correct
4 Correct 180 ms 2088 KB Output is correct
5 Correct 5 ms 1536 KB Output is correct
6 Correct 5 ms 1536 KB Output is correct
7 Correct 5 ms 1536 KB Output is correct
8 Execution timed out 5072 ms 3704 KB Time limit exceeded
9 Halted 0 ms 0 KB -