Submission #961866

# Submission time Handle Problem Language Result Execution time Memory
961866 2024-04-12T15:18:18 Z idas Necklace (Subtask 1-3) (BOI19_necklace1) C++11
9 / 85
1500 ms 600 KB
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define sz(x) ((int)((x).size()))
#define le(vec) vec[vec.size()-1]
#define all(x) (x).begin(), (x).end()
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)

using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;

const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e18;

void setIO()
{
    FAST_IO;
}

void setIO(string s)
{
    FAST_IO;
    freopen((s+".in").c_str(), "r", stdin);
    freopen((s+".out").c_str(), "w", stdout);
}

const int N=3010, C=9973;
string a, b; int n, m;
bool v[N];

string ga(int x, int y)
{
    int ln=y-x+1;

    int in=x, i=x;
    FOR(add, 1, ln+1)
    {
        int j=add%ln+x;
        if(a[j]<a[i]) in=i=j;
        else if(a[j]>a[i]) i=in;
        else i=(i-x+1)%ln+x;
    }

    string ret="";
    FOR(add, 0, ln)
    {
        ret+=a[(in+add-x)%ln+x];
    }

    return ret;
}

string gb(int x, int y)
{
    int ln=y-x+1;

    int in=x, i=x;
    FOR(add, 1, ln+1)
    {
        int j=add%ln+x;
        if(b[j]<b[i]) in=i=j;
        else if(b[j]>b[i]) i=in;
        else i=(i-x+1)%ln+x;
    }

    string ret="";
    FOR(add, 0, ln)
    {
        ret+=b[(in+add-x)%ln+x];
    }

    return ret;
}

string gre(int x, int y)
{
    int ln=y-x+1;

    int in=x, i=x;
    FOR(add, 1, ln+1)
    {
        int j=ln-add+x;
        if(b[j]<b[i]) in=i=j;
        else if(b[j]>b[i]) i=in;
        else i=(i-x-1+ln)%ln+x;
    }

    string ret="";
    FOR(add, 0, ln)
    {
        ret+=b[(in-add-x+ln)%ln+x];
    }

    return ret;
}

int ha(string s)
{
    ll ret=0, pw=1;
    FOR(i, 0, sz(s))
    {
        ret+=(s[i]-'a')*pw%MOD; ret%=MOD;
        pw*=C;
        pw%=MOD;
    }
    return ret;
}

int main()
{
    setIO();
    cin >> a >> b; n=sz(a); m=sz(b);

    for(int ln=n; ln>=1; ln--){
        set<pii> st;
        FOR(i, 0, n)
        {
            int j=i+ln-1; if(j>=n) break;

            string now=ga(i, j);

//            cout << i << " " << j << ": " << now << '\n';

            st.insert({ha(now),i});
        }

        FOR(i, 0, m)
        {
            int j=i+ln-1; if(j>=m) break;

            string now=gb(i, j);

            int hsh=ha(now);
            auto it=st.lower_bound({hsh,0});

            if(it!=st.end() && (*it).f==hsh){
                cout << ln << '\n';
                cout << (*it).s << " " << i;
                return 0;
            }

            now=gre(i, j);

            hsh=ha(now);
            it=st.lower_bound({hsh,0});

            if(it!=st.end() && (*it).f==hsh){
                cout << ln << '\n';
                cout << (*it).s << " " << i;
                return 0;
            }
        }
    }
}
/*
zxyabcd
yxbadctz
*/

Compilation message

necklace.cpp: In function 'void setIO(std::string)':
necklace.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     freopen((s+".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
necklace.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen((s+".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 348 KB Output is partially correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 5 ms 464 KB Output is correct
4 Correct 5 ms 600 KB Output is correct
5 Correct 8 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 348 KB Output is partially correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 5 ms 464 KB Output is correct
4 Correct 5 ms 600 KB Output is correct
5 Correct 8 ms 348 KB Output is correct
6 Correct 28 ms 348 KB Output is correct
7 Correct 36 ms 348 KB Output is correct
8 Partially correct 255 ms 464 KB Output is partially correct
9 Partially correct 427 ms 344 KB Output is partially correct
10 Correct 410 ms 344 KB Output is correct
11 Correct 427 ms 456 KB Output is correct
12 Correct 342 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 348 KB Output is partially correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 5 ms 464 KB Output is correct
4 Correct 5 ms 600 KB Output is correct
5 Correct 8 ms 348 KB Output is correct
6 Correct 28 ms 348 KB Output is correct
7 Correct 36 ms 348 KB Output is correct
8 Partially correct 255 ms 464 KB Output is partially correct
9 Partially correct 427 ms 344 KB Output is partially correct
10 Correct 410 ms 344 KB Output is correct
11 Correct 427 ms 456 KB Output is correct
12 Correct 342 ms 452 KB Output is correct
13 Execution timed out 1538 ms 460 KB Time limit exceeded
14 Halted 0 ms 0 KB -