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: hany01@foxmail.com
************************************************/
#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, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 1000005;
int n, nex[maxn], nex1[maxn], cnt[maxn], cnt1[maxn];
char s[maxn];
inline void getnex()
{
nex[1] = 0, cnt[1] = cnt1[1] = 1;
For(i, 2, n) {
int j = nex[i - 1];
while (j && s[j + 1] != s[i]) j = nex[j];
if (s[j + 1] == s[i]) nex[i] = j + 1; else nex[i] = 0;
cnt[i] = cnt[nex[i]] + 1;
j = nex1[i - 1];
while (j && s[j + 1] != s[i] || (i >> 1) < j + 1) j = nex[j];
if (s[j + 1] == s[i]) nex1[i] = j + 1; else nex1[i] = 0;
cnt1[i] = cnt[nex1[i]] + 1;
}
}
inline void calc()
{
//For(i, 1, n) cout << cnt1[i] << ' ' ;
//cout << endl;
LL Ans = 1;
For(i, 1, 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 + 1), n = strlen(s + 1);
getnex();
calc();
}
return 0;
}
//生当复来归,死当长相思。
// -- 佚名《留别妻》