[JZOJ5178]【NOIP2017提高组模拟6.28】So many prefix?

Description

 

Input

Output

 

Sample Input

样例一:
abababc

样例二:
isdashagayisdashagaydashisnotagaydashisnotagay

Sample Output

样例一:
6

样例二:
30
 

Data Constraint

 

 


 

 

 

设f[i]表示设 f[i] 代表以 i 前缀 ‘s[1]s[2]…s[i]’ 内所有偶数子串出现的次数(包含本身)。

f[i] = f[nxt[i]], i%2==1

f[i] = 1 + f[nxt[i]] , i%2==0

 


 

 

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define reg register
#define ll long long
char a[200005];
int nxt[200005], f[200005];
ll ans;

signed main()
{
    cin >> a + 1;
    int n = strlen(a + 1);
    int k = 0;
    nxt[1] = 0;    
    for(register int i = 2 ; i <= n ; i ++)
    {
        while(k != 0 and a[i] != a[k+1]) k = nxt[k];
        if(a[i] == a[k+1]) k++;
        nxt[i] = k;
    }
    for (reg int i = 1 ; i <= n ; i ++)
        if (i % 2 == 1) f[i] += f[nxt[i]], ans += f[i];
        else f[i] += 1 + f[nxt[i]], ans += f[i];
    cout<<ans<<endl;
    return 0;
    
}

 

posted @ 2018-08-14 19:14  zZhBr  阅读(190)  评论(0编辑  收藏  举报