結果

問題 No.2454 Former < Latter
ユーザー mikammikam
提出日時 2023-09-02 19:52:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 7,109 bytes
コンパイル時間 9,698 ms
コンパイル使用メモリ 458,856 KB
実行使用メモリ 8,700 KB
最終ジャッジ日時 2023-09-02 19:52:41
合計ジャッジ時間 12,200 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 102 ms
8,676 KB
testcase_03 AC 91 ms
8,564 KB
testcase_04 AC 13 ms
8,628 KB
testcase_05 AC 51 ms
8,700 KB
testcase_06 AC 16 ms
8,688 KB
testcase_07 AC 66 ms
8,632 KB
testcase_08 AC 11 ms
4,372 KB
testcase_09 AC 9 ms
4,616 KB
testcase_10 AC 20 ms
5,072 KB
testcase_11 AC 93 ms
8,612 KB
testcase_12 AC 92 ms
8,564 KB
testcase_13 AC 91 ms
8,564 KB
testcase_14 AC 92 ms
8,632 KB
testcase_15 AC 12 ms
6,108 KB
testcase_16 AC 11 ms
5,900 KB
testcase_17 AC 42 ms
4,368 KB
testcase_18 AC 8 ms
4,368 KB
testcase_19 AC 12 ms
8,680 KB
testcase_20 AC 47 ms
8,688 KB
testcase_21 AC 24 ms
4,372 KB
testcase_22 AC 19 ms
4,372 KB
testcase_23 AC 22 ms
4,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
using namespace atcoder;
#include <atcoder/internal_math>
using namespace internal;
#include <bits/stdc++.h>
using namespace std;
#include <boost/multiprecision/cpp_int.hpp>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i,a,b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(),v.end()
#define inc(x,l,r) ((l)<=(x)&&(x)<(r)) 
#define Unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define pcnt __builtin_popcountll
typedef long long ll;
#define int ll
using ld = long double;
using vi = vector<int>;
using vs = vector<string>;
using P = pair<int,int>;
using vp = vector<P>;
using Bint = boost::multiprecision::cpp_int;
template<typename T1,typename T2> bool chmax(T1 &a, const T2 b) {if (a < b) {a = b; return true;} else return false; }
template<typename T1,typename T2> bool chmin(T1 &a, const T2 b) {if (a > b) {a = b; return true;} else return false; }
template<typename T> using priority_queue_greater = priority_queue<T, vector<T>, greater<T>>;
template<typename T1,typename T2> ostream &operator<< (ostream &os, const pair<T1,T2> &p){os << p.first <<" "<<p.second;return os;}
ostream &operator<< (ostream &os, const modint1000000007 &m){os << m.val();return os;}
istream &operator>> (istream &is, modint1000000007 &m){ll in;is>>in;m=in;return is;}
ostream &operator<< (ostream &os, const modint998244353 &m){os << m.val();return os;}
istream &operator>> (istream &is, modint998244353 &m){ll in;is>>in;m=in;return is;}
template<typename T> istream &operator>>(istream& is,vector<T> &v){for(T &in:v)is>>in;return is;}
template<class... T> void input(T&... a){(cin>> ... >> a);}
#ifdef LOCAL
template<typename T> ostream &operator<<(ostream &os,const vector<T> &v){os<<"\x1b[32m";rep(i,v.size())os<<v[i]<<(i+1!=v.size()?" ":"");os<<"\x1b[0m";return os;}
template<class T> int print(T& a){cout << "\x1b[32m"<< a<< '\n' << "\x1b[0m";return 0;}
template<class T,class... Ts> int print(const T&a, const Ts&... b){cout << "\x1b[32m" << a;(cout<<...<<(cout<<' ',b));cout<<'\n' << "\x1b[0m";return 0;}
#else
template<typename T> ostream &operator<<(ostream &os,const vector<T> &v){rep(i,v.size())os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<class T> int print(T& a){cout << a<< '\n';return 0;}
template<class T,class... Ts> int print(const T&a, const Ts&... b){cout << a;(cout<<...<<(cout<<' ',b));cout<<'\n';return 0;}
#endif
#define VI(v,n) vi v(n); input(v)
#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)
#define CHAR(...) char __VA_ARGS__; input(__VA_ARGS__)
int sign(ll x){return x>0?1:x<0?-1:0;}
ll ceil(ll x,ll y){assert(y!=0);if(sign(x)==sign(y))return (x+y-1)/y;return -((-x/y));}
ll floor(ll x,ll y){assert(y!=0);if(sign(x)==sign(y))return x/y;if(y<0)x*=-1,y*=-1;return x/y-(x%y<0);}
ll abs(ll x,ll y){return abs(x-y);}
ll bit(int n){return 1ll<<n;}
template<class T> bool ins(string s,T t){return s.find(t)!=string::npos;}
P operator+ (const P &p, const P &q){ return P{p.first+q.first,p.second+q.second};}
P operator- (const P &p, const P &q){ return P{p.first-q.first,p.second-q.second};}
int yesno(bool ok,string y="Yes",string n="No"){ cout<<(ok?y:n)<<endl;return 0;}
int YESNO(bool ok,string y="YES",string n="NO"){ cout<<(ok?y:n)<<endl;return 0;}
int di[]={-1,0,1,0,-1,-1,1,1};
int dj[]={0,1,0,-1,-1,1,-1,1};
const ll INF = 8e18;
//using mint = modint1000000007;
//using mint = modint998244353;
//mint stom(const string &s,int b=10){mint res = 0;for(auto c:s)res *= b,res += c-'0';return res;}
using ull = unsigned long long;
const ull mod = 0x1fffffffffffffff, base = chrono::duration_cast<chrono::microseconds>(chrono::system_clock::now().time_since_epoch()).count() % mod;
template<bool use_reverse = false>
struct RollingHash {
    vector<ull> hash, hash_rev, base_pow;
    string S;
    string Srev;

    static constexpr ull mask(ll a){ return (1ULL << a) - 1; }
    
    inline ull mul(ull a, ull b) const {
        __uint128_t ans = __uint128_t(a) * b;
        ans = (ans >> 61) + (ans & mod);
        if(ans >= mod) ans -= mod;
        return ans;
    }
    
    
    RollingHash(const string &s):S(s) {
        int n = s.size();
        if(use_reverse){
            Srev = s;
            reverse(Srev.begin(),Srev.end());
            hash_rev.assign(n + 1, 0);
        }
        hash.assign(n + 1, 0);
        base_pow.assign(n + 1, 0);
        base_pow[0] = 1;
        for(int i = 0; i < n; i++) {
            base_pow[i + 1] = mul(base_pow[i], base);
            hash[i + 1] = mul(hash[i], base) + s[i];
            if(hash[i + 1] >= mod) hash[i + 1] -= mod;
        }
        if(use_reverse){
            for(int i = 0; i < n; i++) {
                hash_rev[i + 1] = mul(hash_rev[i], base) + Srev[i];
                if(hash_rev[i + 1] >= mod) hash_rev[i + 1] -= mod;
            }
        }
    }
    
    ull between(int l, int r, bool rev = false) const {
        ull ret;
        if(rev){
            assert(use_reverse);
            ret = hash_rev[hash_rev.size() - l - 1] + mod - mul(hash_rev[hash_rev.size() - r - 1], base_pow[r - l]);
        }
        else ret = hash[r] + mod - mul(hash[l], base_pow[r - l]);
        if(ret >= mod) ret -= mod;
        return ret;
    }
    
    ull connect(ull h1, ull h2, int h2len) const {
        ull ret = mul(h1, base_pow[h2len]) + h2;
        if(ret >= mod) ret -= mod;
        return ret;
    }
    
    void connect(const string &s){
        int n = hash.size() - 1, m = s.size();
        hash.resize(n + m + 1);
        base_pow.resize(n + m + 1);
        for(int i = n; i < n + m; i++) {
            base_pow[i + 1] = mul(base_pow[i], base);
            hash[i + 1] = mul(hash[i], base) + s[i - n];
            if(hash[i + 1] >= mod) hash[i + 1] -= mod;
        }
    }
    bool ispalindrome(int l,int r){
        return between(l,r) == between(l,r,true);
    }
    int lcp(const RollingHash &b, int l1, int r1, int l2, int r2) {
        if(S[l1]!=b.S[l2])return 0;
        int len = min(r1 - l1, r2 - l2);
        int low = -1, high = len + 1;
        while(high - low > 1) {
            int mid = (low + high) / 2;
            if(between(l1, l1 + mid) == b.between(l2, l2 + mid)) low = mid;
            else high = mid;
        }
        return low;
    }
    //[l1,r1) < b.[l2,r2] return -1
    //        =                  0 
    //        >                  1
    int comp(const RollingHash &b,int l1,int r1,int l2,int r2){
        int len = lcp(b,l1,r1,l2,r2);
        if(len == r1-l1){
            if(len == r2-l2)return 0;
            return -1;
        }
        if(len == r2-l2)return 1;
        return (S[l1+len] < b.S[l2+len]?-1:1);
    }
};
signed main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    INT(t);
    while(t--){
        INT(n);
        STR(s);
        RollingHash r(s);
        int res = 0;
        for(int i=1;i<n;i++){
            if(r.comp(r,0,i,i,n)==-1)res++;
        }
        print(res);
    }
    return 0;
}
0