답안 #207211

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
207211 2020-03-06T17:54:22 Z brcode Necklace (Subtask 1-3) (BOI19_necklace1) C++14
85 / 85
490 ms 106872 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3010;
string a,b;
int lss[MAXN][MAXN];
int lsp[MAXN][MAXN];
int lps[MAXN][MAXN];
bool rev;
int maxlen,startA,startB;
void solve(){
    for(int i=0;i<3010;i++){
        for(int j=0;j<3010;j++){
            lss[i][j] = 0;
            lps[i][j] = 0;
            lsp[i][j] = 0;
        }
    }
    for(int j=0;j<b.size();j++){
        for(int i=0;i<a.size() && (i+j)<b.size();i++){
            if(a[i] == b[j+i]){
                if(i){
                    lss[i][j+i] = lss[i-1][j+i-1]+1;
                }else{
                    lss[i][j+i] = 1;
                }
            }else{
                lss[i][j+i] = 0;
            }
        }
    }
    for(int j=0;j<a.size();j++){
        for(int i=0;i<b.size() && j+i<a.size();i++){
            if(a[i+j] == b[i]){
                if(i){
                    lss[i+j][i] = lss[i+j-1][i-1]+1;
                }else{
                    lss[i+j][i] =1;
                }
            }else{
                lss[i+j][i] = 0;
            }
        }
    }
    for(int i=0;i<a.size();i++){
        for(int j=0;j<b.size();j++){
            int x = lss[i][j];
            if(x == 0){
                continue;
            }
            if(j-x>=0){
                lsp[i][j-x] = max(lsp[i][j-x],x);
            }
            if(i-x>=0){
                lps[i-x][j] = max(lps[i-x][j],x);
            }
        }
    }
    for(int i=0;i<a.size();i++){
        for(int j=1;j<b.size();j++){
            lsp[i][j] = max(lsp[i][j],lsp[i][j-1]-1);
        }
    }
    for(int i=0;i<a.size();i++){
        for(int j=0;j<b.size();j++){
            int x = lsp[i][j];
            int y = lps[i][j];
            if(x+y>maxlen){
                maxlen = x+y;
                startA = i-x+1;
                startB = j-y+1;
                if(rev){
                    startB = b.size()-(j+x+1);
                }
            }
        }
    }
}
int main(){
    cin>>a;
    cin>>b;
    solve();
    reverse(b.begin(),b.end());
    rev = 1;
    solve();
    cout<<maxlen<<endl;
    cout<<startA<<" "<<startB<<endl;
}

Compilation message

necklace.cpp: In function 'void solve()':
necklace.cpp:19:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<b.size();j++){
                 ~^~~~~~~~~
necklace.cpp:20:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<a.size() && (i+j)<b.size();i++){
                     ~^~~~~~~~~
necklace.cpp:20:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<a.size() && (i+j)<b.size();i++){
                                   ~~~~~^~~~~~~~~
necklace.cpp:32:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<a.size();j++){
                 ~^~~~~~~~~
necklace.cpp:33:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<b.size() && j+i<a.size();i++){
                     ~^~~~~~~~~
necklace.cpp:33:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<b.size() && j+i<a.size();i++){
                                   ~~~^~~~~~~~~
necklace.cpp:45:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<a.size();i++){
                 ~^~~~~~~~~
necklace.cpp:46:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j=0;j<b.size();j++){
                     ~^~~~~~~~~
necklace.cpp:59:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<a.size();i++){
                 ~^~~~~~~~~
necklace.cpp:60:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j=1;j<b.size();j++){
                     ~^~~~~~~~~
necklace.cpp:64:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<a.size();i++){
                 ~^~~~~~~~~
necklace.cpp:65:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j=0;j<b.size();j++){
                     ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 106744 KB Output is correct
2 Correct 78 ms 106616 KB Output is correct
3 Correct 77 ms 106744 KB Output is correct
4 Correct 80 ms 106744 KB Output is correct
5 Correct 79 ms 106744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 106744 KB Output is correct
2 Correct 78 ms 106616 KB Output is correct
3 Correct 77 ms 106744 KB Output is correct
4 Correct 80 ms 106744 KB Output is correct
5 Correct 79 ms 106744 KB Output is correct
6 Correct 82 ms 106616 KB Output is correct
7 Correct 83 ms 106744 KB Output is correct
8 Correct 79 ms 106744 KB Output is correct
9 Correct 80 ms 106744 KB Output is correct
10 Correct 81 ms 106744 KB Output is correct
11 Correct 80 ms 106740 KB Output is correct
12 Correct 79 ms 106744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 106744 KB Output is correct
2 Correct 78 ms 106616 KB Output is correct
3 Correct 77 ms 106744 KB Output is correct
4 Correct 80 ms 106744 KB Output is correct
5 Correct 79 ms 106744 KB Output is correct
6 Correct 82 ms 106616 KB Output is correct
7 Correct 83 ms 106744 KB Output is correct
8 Correct 79 ms 106744 KB Output is correct
9 Correct 80 ms 106744 KB Output is correct
10 Correct 81 ms 106744 KB Output is correct
11 Correct 80 ms 106740 KB Output is correct
12 Correct 79 ms 106744 KB Output is correct
13 Correct 490 ms 106760 KB Output is correct
14 Correct 421 ms 106744 KB Output is correct
15 Correct 485 ms 106872 KB Output is correct
16 Correct 431 ms 106872 KB Output is correct
17 Correct 378 ms 106744 KB Output is correct
18 Correct 387 ms 106744 KB Output is correct
19 Correct 421 ms 106744 KB Output is correct
20 Correct 441 ms 106864 KB Output is correct
21 Correct 460 ms 106744 KB Output is correct