답안 #976336

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
976336 2024-05-06T12:45:10 Z roctes7 Necklace (Subtask 1-3) (BOI19_necklace1) C++17
0 / 85
2 ms 860 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
const double eps = 1e-6;
const int MAX = 3e3+3;

int pi[3003][7003];
int n;
string s;


void kmp(int sta){
    for (int i = 1; i < n; i++){
        int j = pi[sta][i-1];
        while (j > 0 && s[i] != s[j])
            j = pi[sta][j-1];
        if (s[i] == s[j])
            j++;
        pi[sta][i] = j;
    }
}

string t1,t2;
pair<int,pair<int,int>> solve(){
       for(int i=0;i<t1.size();i++){
        s = "";
        for(int j=i;j<t1.size();j++)s+=t1[j];
        for(int j=0;j<i;j++)s+=t1[j];
        s+="#";
        s+=t2;
        n = s.size();
        kmp(i);
        //cout<<s<<endl;
   }


   int l = 1;
   int r = t1.size();

   int ans = -1;
   int ansl = -1;
   int ansr = -1;

   while(l<=r){
        int k = (l+r)/2;
        bool check = false;
        for(int i=0;i<t1.size();i++){
            if(check)break;
            for(int j=t1.size()+1;j<n;j++){
                int cur = pi[i][j];
                if(cur>=k){
                    check = true;
                    if(cur>ans){
                        ans = cur;
                        ansl = i;
                        ansr = j-(t1.size()+1)-ans+1;
                    }
                    break;
                }
                int stil = k - cur;
                if(i+cur>=t1.size()||j-cur<t1.size()+1)continue;

                int cur2 = pi[i+cur][j-cur];
                if(cur2>=stil){
                    check = true;
                    if(cur+cur2>ans){
                        ans = cur+cur2;
                        ansl = i;
                        ansr = j-(t1.size()+1)-ans+1;
                    }
                    break;
                }
            }
        }

        if(check){
            l = k+1;
        }else r = k-1;
   }
   return {ans,{ansl,ansr}};
}


int main(){
    fastio;



   cin>>t1>>t2;



     auto ans1 = solve();


    reverse(t2.begin(),t2.end());

    auto ans2 = solve();
    ans2.se.se = ans2.se.se + ans2.fi-1;
    ans2.se.se = t2.size()-ans2.se.se-1;

    if(ans2.se.se<0)ans2.se.se = t2.size()+ans2.se.se;

    reverse(t2.begin(),t2.end());
    reverse(t1.begin(),t1.end());

    auto ans3 = solve();
    ans3.se.fi = ans3.se.fi + ans3.fi-1;
    ans3.se.fi = t1.size()-ans3.se.fi-1;
    if(ans3.se.fi<0)ans3.se.fi = t1.size()+ans3.se.fi;

    swap(t1,t2);

    auto ans4 = solve();
    ans4.se.se = ans4.se.se + ans4.fi-1;
    ans4.se.se = t2.size()-ans4.se.se-1;
    if(ans4.se.se<0)ans4.se.se = t2.size()+ans4.se.se;
    swap(ans4.se.se,ans4.se.fi);

    reverse(t2.begin(),t2.end());
    reverse(t1.begin(),t1.end());

    auto ans5 = solve();
    ans5.se.fi = ans5.se.fi + ans5.fi-1;
    ans5.se.fi = t2.size()-ans5.se.fi-1;
    if(ans5.se.fi<0)ans5.se.fi = t2.size()+ans5.se.fi;
    swap(ans5.se.se,ans5.se.fi);


    pair<int,pair<int,int>> ans = max({ans1,ans2,ans3,ans4,ans5});



    cout<<ans.fi<<endl<<ans.se.fi<<" "<<ans.se.se;

    return 0;
}

Compilation message

necklace.cpp: In function 'std::pair<int, std::pair<int, int> > solve()':
necklace.cpp:30:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |        for(int i=0;i<t1.size();i++){
      |                    ~^~~~~~~~~~
necklace.cpp:32:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |         for(int j=i;j<t1.size();j++)s+=t1[j];
      |                     ~^~~~~~~~~~
necklace.cpp:52:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |         for(int i=0;i<t1.size();i++){
      |                     ~^~~~~~~~~~
necklace.cpp:66:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |                 if(i+cur>=t1.size()||j-cur<t1.size()+1)continue;
      |                    ~~~~~^~~~~~~~~~~
necklace.cpp:66:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |                 if(i+cur>=t1.size()||j-cur<t1.size()+1)continue;
      |                                      ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Incorrect 1 ms 860 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Incorrect 1 ms 860 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Incorrect 1 ms 860 KB Output isn't correct
5 Halted 0 ms 0 KB -