Submission #643957

# Submission time Handle Problem Language Result Execution time Memory
643957 2022-09-23T09:53:58 Z mychecksedad Necklace (Subtask 1-3) (BOI19_necklace1) C++17
5 / 85
1500 ms 16076 KB
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;


int n, m;
string a, b;
ll h[N], h1[N], e[N], re[N];

ll po(ll a, ll b){ll res = 1; while(b){if(b&1) (res*=a)%=MOD; b>>=1; (a*=a)%=MOD;} return res;}

bool is_equal(string x, string y){

    h[0] = h1[0] = 0;

    for(int i = 1; i <= x.length(); ++i) h[i] = (h[i - 1] + e[i] * (x[i - 1] - 'a' + 1)) % MOD;
    for(int i = 1; i <= x.length(); ++i) h1[i] = (h1[i - 1] + e[i] * (y[i - 1] - 'a' + 1)) % MOD;

    for(int c = 0; c < x.length(); ++c){
        ll f = ((h1[x.length()] - h1[c] + MOD) * re[c] % MOD) + (h1[c] * e[c]);
        f %= MOD;
        if(f == h[x.length()]){
            return 1;
        }
    }

    reverse(all(y));

    for(int i = 1; i <= x.length(); ++i) h1[i] = (h1[i - 1] + e[i] * (y[i - 1] - 'a' + 1)) % MOD;

    for(int c = 0; c < x.length(); ++c){
        ll f = ((h1[x.length()] - h1[c] + MOD) * re[c] % MOD) + (h1[c] * e[c]);
        f %= MOD;
        if(f == h[x.length()]){
            return 1;
        }
    }

    return 0;
}

void solve(){
    cin >> a >> b;
    n = a.length();
    m = b.length();

    e[0] = 1;
    for(int i = 1; i < N; ++i) e[i] = (e[i - 1] * 31) % MOD;
    re[N-1]=po(e[N-1], MOD-2);
    for(int i = N-2; i >= 0; --i) re[i] = (re[i + 1] * 31) % MOD;

    int len = 0, i1 = 0, i2 = 0;
    for(int i = 0; i < n; ++i){
        for(int j = i; j < n; ++j){
            int l = j - i;
            for(int k = 0; k < m - l; ++k){
                if(is_equal(a.substr(i, l + 1), b.substr(k, l + 1))){
                    if(len<l+1){
                        len=l+1, i1=i, i2=k;
                    }
                }
            }
        }
    }
    cout << len << '\n' << i1 << ' ' << i2 ;
}





int main(){
    cin.tie(0); ios::sync_with_stdio(0);
    int T = 1, aa;
    // cin >> T;aa=T;
    while(T--){
        // cout << "Case #" << aa-T << ": ";
        solve();
        cout << '\n';
    }
    return 0;
 
}

Compilation message

necklace.cpp: In function 'bool is_equal(std::string, std::string)':
necklace.cpp:26:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for(int i = 1; i <= x.length(); ++i) h[i] = (h[i - 1] + e[i] * (x[i - 1] - 'a' + 1)) % MOD;
      |                    ~~^~~~~~~~~~~~~
necklace.cpp:27:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |     for(int i = 1; i <= x.length(); ++i) h1[i] = (h1[i - 1] + e[i] * (y[i - 1] - 'a' + 1)) % MOD;
      |                    ~~^~~~~~~~~~~~~
necklace.cpp:29:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for(int c = 0; c < x.length(); ++c){
      |                    ~~^~~~~~~~~~~~
necklace.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for(int i = 1; i <= x.length(); ++i) h1[i] = (h1[i - 1] + e[i] * (y[i - 1] - 'a' + 1)) % MOD;
      |                    ~~^~~~~~~~~~~~~
necklace.cpp:41:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for(int c = 0; c < x.length(); ++c){
      |                    ~~^~~~~~~~~~~~
necklace.cpp: In function 'int main()':
necklace.cpp:84:16: warning: unused variable 'aa' [-Wunused-variable]
   84 |     int T = 1, aa;
      |                ^~
# Verdict Execution time Memory Grader output
1 Partially correct 155 ms 15976 KB Output is partially correct
2 Partially correct 177 ms 15956 KB Output is partially correct
3 Partially correct 137 ms 16076 KB Output is partially correct
4 Correct 153 ms 16076 KB Output is correct
5 Partially correct 206 ms 15956 KB Output is partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 155 ms 15976 KB Output is partially correct
2 Partially correct 177 ms 15956 KB Output is partially correct
3 Partially correct 137 ms 16076 KB Output is partially correct
4 Correct 153 ms 16076 KB Output is correct
5 Partially correct 206 ms 15956 KB Output is partially correct
6 Execution timed out 1590 ms 15984 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Partially correct 155 ms 15976 KB Output is partially correct
2 Partially correct 177 ms 15956 KB Output is partially correct
3 Partially correct 137 ms 16076 KB Output is partially correct
4 Correct 153 ms 16076 KB Output is correct
5 Partially correct 206 ms 15956 KB Output is partially correct
6 Execution timed out 1590 ms 15984 KB Time limit exceeded
7 Halted 0 ms 0 KB -