結果

問題 No.2454 Former < Latter
ユーザー Taiki0715Taiki0715
提出日時 2023-09-01 22:29:26
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 3,510 bytes
コンパイル時間 7,857 ms
コンパイル使用メモリ 187,872 KB
実行使用メモリ 8,792 KB
最終ジャッジ日時 2025-01-03 09:39:53
合計ジャッジ時間 10,559 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
template<int mod>istream &operator>>(istream &is,static_modint<mod> &a){long long b;is>>b;a=b;return is;}
istream &operator>>(istream &is,modint &a){long long b;cin>>b;a=b;return is;}
#endif
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}
template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}
template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}
template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}
template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}
#define reps(i,a,n) for(int i=(a);i<(n);i++)
#define rep(i,n) reps(i,0,n)
#define all(x) x.begin(),x.end()
#define pcnt(x) __builtin_popcount(x)
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<typename T,size_t n,size_t id=0>
auto vec(const int (&d)[n],const T &init=T()){
  if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));
  else return init;
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) static_cast<void>(0)
template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}
#endif
void SOLVE();
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  #ifdef LOCAL
  clock_t start=clock();
  #endif
  int testcase=1;
  cin>>testcase;
  for(int i=0;i<testcase;i++){
    SOLVE();
  }
  #ifdef LOCAL
  cout<<"time:";
  cout<<(clock()-start)/1000;
  cout<<"ms\n";
  #endif
}
struct RollingHash{
  vector<ull>hashed,power;
  const ull MOD=(1ull<<61)-1;
  const ull MASK31=(1ull<<31)-1;
  const ull MASK30=(1ull<<30)-1;
  ull mul(ull a,ull b){
    ull au=a>>31;
    ull ad=a&MASK31;
    ull bu=b>>31;
    ull bd=b&MASK31;
    ull mid=au*bd+ad*bu;
    ull midu=mid>>30;
    ull midd=mid&MASK30;
    return (au*bu*2+midu+(midd<<31)+ad*bd)%MOD;
  }
  RollingHash(const string s,ull base=10007){
    int sz=(int)s.size();
    hashed.assign(sz+1,0);
    power.assign(sz+1,0);
    power[0]=1;
    rep(i,sz){
      power[i+1]=mul(power[i],base);
      hashed[i+1]=mul(hashed[i],base)+s[i];
      if(hashed[i+1]>=MOD)hashed[i+1]-=MOD;
    }
  }
  ull get(int l,int r){
    ull ret=hashed[r]+MOD-mul(hashed[l],power[r-l]);
    if(ret>=MOD)ret-=MOD;
    return ret;
  }
  ull connect(ull h1,ull h2,int h2len){
    ull ret=mul(h1,power[h2len])+h2;
    if(ret>=MOD)ret-=MOD;
    return ret;
  }
  int lcp(RollingHash &rl,int l1,int r1,int l2,int r2){
    int len=min(r1-l1,r2-l2);
    int low=-1,high=len+1;
    while(high-low>1){
      int mid=(low+high)/2;
      if(get(l1,l1+mid)==rl.get(l2,l2+mid))low=mid;
      else high=mid;
    }
    return low;
  }
};
void SOLVE(){
  int n;
  string s;
  cin>>n>>s;
  RollingHash r(s);
  int ans=0;
  reps(i,1,n){
    int l=r.lcp(r,0,i,i,n);
    if(i==l&&n-i==l){
      continue;
    }
    else if(i==l){
      ans++;
    }
    else if(n-i==l)continue;
    else{
      ans+=s[l]<s[i+l];
    }
  }
  cout<<ans<<endl;
}
0