天天看點

【UOJ5】【BZOJ3670】【NOI2014】動物園(KMP)

Description

近日,園長發現動物園中好吃懶做的動物越來越多了。例如企鵝,隻會賣萌向遊客要吃的。為了整治動物園的不良風氣,讓動物們憑自己的真才實學向遊客要吃的,園長決定開設算法班,讓動物們學習算法。

某天,園長給動物們講解KMP算法。

園長:“對于一個字元串S,它的長度為L。我們可以在O(L)的時間内,求出一個名為next的數組。有誰預習了next數組的含義嗎?”

熊貓:“對于字元串S的前i個字元構成的子串,既是它的字尾又是它的字首的字元串中(它本身除外),最長的長度記作next[i]。”

園長:“非常好!那你能舉個例子嗎?”

熊貓:“例S為abcababc,則next[5]=2。因為S的前5個字元為abcab,ab既是它的字尾又是它的字首,并且找不到一個更長的字元串滿足這個性質。同理,還可得出next[1] = next[2] = next[3] = 0,next[4] = next[6] = 1,next[7] = 2,next[8] = 3。”

園長表揚了認真預習的熊貓同學。随後,他詳細講解了如何在O(L)的時間内求出next數組。

下課前,園長提出了一個問題:“KMP算法隻能求出next數組。我現在希望求出一個更強大num數組一一對于字元串S的前i個字元構成的子串,既是它的字尾同時又是它的字首,并且該字尾與該字首不重疊,将這種字元串的數量記作num[i]。例如S為aaaaa,則num[4] = 2。這是因為S的前4個字元為aaaa,其中a和aa都滿足性質‘既是字尾又是字首’,同時保證這個字尾與這個字首不重疊。而aaa雖然滿足性質‘既是字尾又是字首’,但遺憾的是這個字尾與這個字首重疊了,是以不能計算在内。同理,num[1] = 0,num[2] = num[3] = 1,num[5] = 2。”

最後,園長給出了獎勵條件,第一個做對的同學獎勵巧克力一盒。聽了這句話,睡了一節課的企鵝立刻就醒過來了!但企鵝并不會做這道題,于是向參觀動物園的你尋求幫助。你能否幫助企鵝寫一個程式求出num數組呢?

Solution

KMP亂搞。

nex1[i]表示符合要求(即字首字尾不重疊)的nex[i]

用cnt[i]表示s[1…i]有多少個子串既是字首又是字尾,cnt1[i]表示滿足要求的既是字首又是字尾的子串個數。

那麼有:cnt[i] = cnt[nex[i]] + 1;cnt1[i] = cnt[nex1[i]] + 1

直接亂艹即可。

Source

/************************************************
 * Au: Hany01
 * Date: Mar 29th, 2018
 * Prob: [BZOJ3670][NOI2014] 動物園
 * Email: [email protected]
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b,  : ; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b,  : ; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = , __ = , c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << ) + (_ << ) + (c_ ^ );
    return _ * __;
}

const int maxn = ;

int n, nex[maxn], nex1[maxn], cnt[maxn], cnt1[maxn];
char s[maxn];

inline void getnex()
{
    nex[] = , cnt[] = cnt1[] = ;
    For(i, , n) {
        int j = nex[i - ];
        while (j && s[j + ] != s[i]) j = nex[j];
        if (s[j + ] == s[i]) nex[i] = j + ; else nex[i] = ;
        cnt[i] = cnt[nex[i]] + ;
        j = nex1[i - ];
        while (j && s[j + ] != s[i] || (i >> ) < j + ) j = nex[j];
        if (s[j + ] == s[i]) nex1[i] = j + ; else nex1[i] = ;
        cnt1[i] = cnt[nex1[i]] + ;
    }
}

inline void calc()
{
    //For(i, 1, n) cout << cnt1[i] << ' ' ;
    //cout << endl;
    LL Ans = ;
    For(i, , n) (Ans *= (LL)cnt1[i]) %= Mod;
    printf("%lld\n", Ans);
}

int main()
{
#ifdef hany01
    File("bzoj3670");
#endif

    for (static int T = read(); T --; )
    {
        scanf("%s", s + ), n = strlen(s + );
        getnex();
        calc();
    }

    return ;
}
//生當複來歸,死當長相思。
//    -- 佚名《留别妻》