結果

問題 No.465 PPPPPPPPPPPPPPPPAPPPPPPPP
ユーザー nola_suznola_suz
提出日時 2016-12-16 19:57:59
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 8,939 bytes
コンパイル時間 1,333 ms
コンパイル使用メモリ 112,928 KB
実行使用メモリ 46,676 KB
最終ジャッジ日時 2023-08-20 09:10:21
合計ジャッジ時間 27,063 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,680 KB
testcase_01 AC 4 ms
11,708 KB
testcase_02 AC 5 ms
15,780 KB
testcase_03 AC 3 ms
11,648 KB
testcase_04 AC 4 ms
11,660 KB
testcase_05 AC 297 ms
14,680 KB
testcase_06 AC 1,702 ms
37,280 KB
testcase_07 AC 319 ms
19,172 KB
testcase_08 AC 1,679 ms
36,428 KB
testcase_09 AC 1,890 ms
43,252 KB
testcase_10 AC 1,876 ms
43,104 KB
testcase_11 AC 1,724 ms
39,260 KB
testcase_12 AC 1,238 ms
33,440 KB
testcase_13 AC 915 ms
26,556 KB
testcase_14 AC 1,763 ms
37,720 KB
testcase_15 RE -
testcase_16 AC 1,808 ms
45,724 KB
testcase_17 AC 1,788 ms
45,820 KB
testcase_18 RE -
testcase_19 AC 1,845 ms
46,676 KB
testcase_20 AC 1,827 ms
37,700 KB
testcase_21 RE -
32_ratsliveonnoevilstar.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

template<class T>
struct RollingHash {
    static const int NN = 1; // the number of hash table
    T base[2] = {1009, 1007};
    T PMOD[2] = {1000000007, 1000000009};
    vector<T> hash[2];
    vector<T> mypow[2];
    int sz;
    void init(string s) {
        sz = s.size();
        for(int i = 0; i < NN; i++) {
            hash[i].assign(sz + 1, 0);
            mypow[i].assign(sz + 1, 0);
            mypow[i][0] = 1;
        }
        for(int i = 0; i < NN; i++) {
            for(int j = 0; j < sz; j++) {
                hash[i][j + 1] = (hash[i][j] * base[i] + s[j]) % PMOD[i];
                mypow[i][j + 1] = mypow[i][j] * base[i] % PMOD[i];
            }
        }
    }
    RollingHash() {
    }
    // hash[t] value of s[l..r]
    T get(int l, int r, int t) {
        T ret = (hash[t][r + 1] - hash[t][l] * mypow[t][r - l + 1] % PMOD[t] + PMOD[t]) % PMOD[t];
        return ret;
    }
    // compare s[a..b] and s[c..d] O(1)
    bool comp(int a, int b, int c, int d) {
        if(a < 0 || sz <= b || c < 0 || sz <= d) return false;
        for(int i = 0; i < NN; i++) {
            if(get(a, b, i) != get(c, d, i)) return false;
        }
        return true;
    }
    // longest common extension s[l..LCE(l,r)] == s[r..LCE(l,r)] && s[l..LCE(l,r)+1] != s[r..LCE(l,r)+1]
    int LCE(int l,int r){
        tie(l,r)=minmax(l,r);
        if(l==r) return sz-r+1;
        int ll = 0;
        int rr = sz;
        while(rr-ll>1){
            int mid = (ll+rr)>>1;
            if(comp(l,l+mid-1,r,r+mid-1)) ll=mid;
            else rr=mid;
        }
        return ll;
    }
};

RollingHash<ll> rh;
string s;
int n;

int LCE(int l, int r){
    return rh.LCE(l, r);
}


static const int MAX_SIZE = 1 << 20; //segment tree のサイズ。 2^17 ≒ 1.3 * 10^5

ll all[2 * MAX_SIZE - 1], part[2 * MAX_SIZE - 1]; // segment tree

//区間[a, b)に値xを加算する.
void add(int a, int b, ll x, int k = 0, int l = 0, int r = MAX_SIZE)
{
    if (a <= l && r <= b){ //[l, r)が[a, b)に完全に内包されていれば
        all[k] += x; //[l, r)の全ての区間が持つ値としてxを足す.
    }
    else if (l < b && a < r){ //[l, r)と[a, b)が交差していれば
        part[k] += (min(b, r) - max(a, l)) * x;  //交差している分の値を, 部分的な和を持つノードに加算する.
        add(a, b, x, k * 2 + 1, l, (l + r) / 2); //子でも同じ処理を行う.
        add(a, b, x, k * 2 + 2, (l + r) / 2, r); //〃.
    }
}

ll sum(int a, int b, int k = 0, int l = 0, int r = MAX_SIZE)
{
    if (b <= l || r <= a){ //[a, b)と[l, r)が全く交差しない場合
        return (0);
    }
    else if (a <= l && r <= b){ //完全に内包されていれば
        return (all[k] * (r - l) + part[k]);
    }
    else { //[l, r)と[a, b)が交差していれば
        ll res;
        res = (min(b, r) - max(a, l)) * all[k]; //そのノードの全ての要素が持つ値のうち, [a, b)に属すものの分だけを加算する.
        res += sum(a, b, k * 2 + 1, l, (l + r) / 2); //子ノードで和を求める.
        res += sum(a, b, k * 2 + 2, (l + r) / 2, r); //〃
        return (res);
    }
}

bool isPal(string a){
    string b = a;
    reverse(ALL(b));
    return a == b;
}

int func(string a){
    int ret = 0;
    rep(i,a.size()-1){
        if(isPal(a.substr(0,i+1))&&isPal(a.substr(i+1))) {
            ret++;
        }
    }
    return ret;
}

// int ff(string a){
//  int n = a.size();
//  rep(i,n){
//      rep(j,i){
//          for(int k=i+2)
//      }
//  }
// }

vector<pair<pii,int>> se;

void mainmain(){
    cin>>s;
    n = s.size();
    string r = s;
    reverse(ALL(r));
    rh.init(s+'$'+r);
    vll a(n);
    rep(i,n-1){
        int le = i+1;
        if(le&1){
            int tl = le/2;
            int c = i-tl;
            if(tl == LCE(c+1, 2*n-(c-1))) a[i+1]=1;
        }
        else{
            int tl = le/2;
            int c = i-tl;
            if(tl == LCE(c+1, 2*n-c)) a[i+1]=1;
        }
    }
    // rep(i,n){
    //  cout<<a[i];
    // }cout<<endl;
    se.PB({{-INF,-1}, -INF});
    rep(i,n){
        int l=i;
        while(i<n&&a[l]==a[i]) i++;
        i--;
        // cout<<l<<" "<<i<<" "<<a[l]<<endl;
        se.PB({{l,i}, a[l]});
    }
    se.PB({{n,INF}, INF});
    assert(se.size()<1000);
    rep(i,n){
        // cout<<i<<endl;
        if(i+1<n){
            // even pal
            int le = LCE(i+1, 2*n-i);
            if(le){
                int l = i-le+1;
                int r = i;
                // if(i==5) cout<<l<<" "<<r<<endl;
                auto it = lower_bound(ALL(se), mkp(pii(r, INF), 1));
                it--;
                while(1){
                    // cout<<"hoge"<<endl;
                    int ll,rr,t;
                    ll=it->F.F,rr=it->F.S,t=it->S;
                    // if(i==5) cout<<ll<<" "<<rr<<" "<<t<<endl;
                    if(rr<l) break;
                    if(t){
                        int tl = max(l, ll);
                        int tr = min(r, rr);
                        int tle = i-tr;
                        // if(i==5){
                        //  cout<<i+tle+1<<" "<<i+tle+1+(tr-tl) << endl;
                        // }
                        add(i+tle+1, i+tle+2+(tr-tl), 1);
                    }
                    // cout<<"hoge"<<endl;
                    if(it == se.begin()) break;
                    it--;
                }
            }
        }
        // odd pal
        int le = (i==0?0:LCE(i+1, 2*n-(i-1)));
        int l = i-le;
        int r = i;
        auto it = lower_bound(ALL(se), mkp(pii(r,INF),1));
        it--;
        while(1){
            int ll,rr,t;
            ll=it->F.F,rr=it->F.S,t=it->S;
            // if(i==1) cout<<ll<<" "<<rr<<" "<<t<<endl;
            if(rr<l) break;
            if(t){
                int tl = max(l, ll);
                int tr = min(r, rr);
                int tle = i-tr;
                add(i+tle, i+tle+1+(tr-tl), 1);
            }
            if(it == se.begin()) break;
            it--;
        }
    }
    vll b(n);
    rep(i,n-1){
        b[i] = sum(i,i+1);
    }
    vll c(n);
    rep(i,n-1){
        int le = i+1;
        if(le&1){
            int tl = le/2;
            int ce = n-1-tl;
            if(tl == LCE(ce+1, 2*n-(ce-1))) c[n-1-i]=1;
        }
        else{
            int tl = le/2;
            int ce = n-1-tl;
            if(tl == LCE(ce+1, 2*n-ce)) c[n-1-i]=1;
        }
    }
    // rep(i,n){
    //  cout<<c[i];
    // }cout<<endl;
    // rep(i,n){
    //  if(b[i]){
    //      int t = func(s.substr(0,i+1));
    //      if(t != b[i]){
    //          cout<<"error "<<i<<" "<<t<<endl;
    //      }
    //  }
    // }
    for(int i=n-2;i>=0;i--){
        c[i] += c[i+1];
    }
    ll ans = 0;
    rep(i,n-2){
        ans += b[i] * c[i+2];
    }
    cout << ans << endl;
    // cout<<ff(s)<<endl;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}
0