結果

問題 No.2454 Former < Latter
ユーザー Taiki0715Taiki0715
提出日時 2023-09-01 22:29:26
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 3,510 bytes
コンパイル時間 6,198 ms
コンパイル使用メモリ 151,860 KB
実行使用メモリ 8,640 KB
最終ジャッジ日時 2023-09-01 22:29:40
合計ジャッジ時間 9,852 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 194 ms
8,532 KB
testcase_03 AC 183 ms
8,572 KB
testcase_04 AC 183 ms
8,628 KB
testcase_05 AC 184 ms
8,572 KB
testcase_06 AC 181 ms
8,516 KB
testcase_07 AC 181 ms
8,508 KB
testcase_08 AC 82 ms
4,380 KB
testcase_09 AC 107 ms
4,676 KB
testcase_10 AC 118 ms
4,796 KB
testcase_11 AC 183 ms
8,640 KB
testcase_12 AC 181 ms
8,640 KB
testcase_13 AC 180 ms
8,632 KB
testcase_14 AC 185 ms
8,628 KB
testcase_15 AC 157 ms
6,112 KB
testcase_16 AC 159 ms
6,028 KB
testcase_17 AC 242 ms
4,380 KB
testcase_18 AC 56 ms
4,380 KB
testcase_19 AC 178 ms
8,528 KB
testcase_20 AC 182 ms
8,572 KB
testcase_21 AC 83 ms
4,384 KB
testcase_22 AC 108 ms
4,380 KB
testcase_23 AC 107 ms
4,712 KB
権限があれば一括ダウンロードができます

ソースコード

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