Problem:

洛谷3546

Analysis:

I gave up and saw other's solution when I had nearly thought of the method ... What a pity

Let's define a border of string \(s\) as a prefix \(p\) of \(s\) that \(p\) is also a suffix of \(s\), and \(p\) is not longer than half of \(s\). What the problem asked us to look for is a number \(L\), that the prefix of length \(L\) can be divided into two string \(s1\) and \(s2\) , and the suffix of length \(L\) can be divided into two string \(s2\) and \(s1\), so that this pair of prefix and suffix is cyclically equivalent. Obviously, \(s1\) is a border of string \(s\). Another fact is, if \(s1\) is of length \(len\), \(s2\) is a border of the substring \([len, n - len - 1]\). Define \(f[len]\) as the length of the maximum border of the substring \([len, n - len - 1]\) . Let's enumerate the length of \(s1\) as \(len\) brutely, and for all legal \(len\) ("legal" means the prefix of length \(len\) is a border of \(s\). We can check it by hashing in \(O(1)\) time), the answer is \(len + f[len]\).

Now the problem is how to calculate \(f[len]\). Brute force takes \(O(n^2)\) complexity, but the useful fact below can decrease the complexity to \(O(n)\) :

\[f[i]\leq f[i+1]+2
\]

To make it easy, look at the (beautiful) picture below.

The first picture shows the situation when \(f[i]=f[i+1]+2\), and the second picture shows if \(f[i]\) (the black ones) is more than \(f[i+1]\) (the red ones) plus \(2\) , the \(f[i+1]\) must be wrong, for there's a longer border (the blue ones) of substring \([i+1, n-i-2]\).

Because of this fact, we can solve \(f[len]\) in \(O(n)\) time. We initialize \(f[i]\) as \(f[i+1]+2\), and decrease it until the substring \([i, n-i-1]\) has a border of length \(f[i]\). The proof of the complexity is similar to the one of solving \(height\) array by Suffix Array.

Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std; namespace zyt
{
template<typename T>
inline bool read(T &x)
{
char c;
bool f = false;
x = 0;
do
c = getchar();
while (c != EOF && c != '-' && !isdigit(c));
if (c == EOF)
return false;
if (c == '-')
f = true, c = getchar();
do
x = x * 10 + c - '0', c = getchar();
while (isdigit(c));
if (f)
x = -x;
return true;
}
inline bool read(char *const s)
{
return ~scanf("%s", s);
}
template<typename T>
inline void write(T x)
{
static char buf[20];
char *pos = buf;
if (x < 0)
putchar('-'), x = -x;
do
*pos++ = x % 10 + '0';
while (x /= 10);
while (pos > buf)
putchar(*--pos);
}
const int N = 1e6 + 10;
int f[N], n;
// f[i] is the maximum length of the border of substr[i, n - i - 1]
char str[N];
namespace Hash
{
typedef long long ll;
typedef pair<int, int> pii;
typedef pii hash_t;
hash_t h[N], pow[N];
const hash_t seed = hash_t(61, 67), p = hash_t(1e9 + 7, 1e9 + 9);
hash_t operator + (const hash_t &a, const hash_t &b)
{
return hash_t((a.first + b.first) % p.first, (a.second + b.second) % p.second);
}
hash_t operator - (const hash_t &a, const hash_t &b)
{
return hash_t((a.first - b.first + p.first) % p.first,
(a.second - b.second + p.second) % p.second);
}
hash_t operator * (const hash_t &a, const hash_t &b)
{
return hash_t(int((ll)a.first * b.first % p.first),
int((ll)a.second * b.second % p.second));
}
void init()
{
pow[0] = make_pair(1, 1);
for (int i = 1; i < N; i++)
pow[i] = pow[i - 1] * seed;
}
inline int ctoi(const char c)
{
return c - 'a';
}
void get(const char *const s)
{
h[0] = make_pair(ctoi(s[0]), ctoi(s[0]));
for (int i = 1; i < n; i++)
h[i] = h[i - 1] * seed + make_pair(ctoi(s[i]), ctoi(s[i]));
}
hash_t extract(const int l, const int r)
{
return l ? (h[r] - h[l - 1] * pow[r - l + 1]) : h[r];
}
}
using namespace Hash;
void mk_f()
{
f[n >> 1] = 0;
for (int i = (n >> 1) - 1; i >= 0; i--)
{
f[i] = min(f[i + 1] + 2, (n >> 1) - i);
while (f[i] && extract(i, i + f[i] - 1) != extract(n - i - f[i], n - i - 1))
--f[i];
}
}
int work()
{
read(n), read(str);
init();
get(str);
mk_f();
int ans = 0;
for (int i = 1; i <= (n >> 1); i++)
if (extract(0, i - 1) == extract(n - i, n - 1))
ans = max(ans, i + f[i]);
write(ans);
return 0;
}
}
int main()
{
return zyt::work();
}

【洛谷3546_BZOJ2803】[POI2012]PRE-Prefixuffix(String Hash)的更多相关文章

  1. 洛谷P3538 [POI2012]OKR-A Horrible Poem [字符串hash]

    题目传送门 A Horrible Poem 题目描述 Bytie boy has to learn a fragment of a certain poem by heart. The poem, f ...

  2. 洛谷P3237 米特运输 [HNOI2014] hash/二进制分解

    正解:hash/二进制分解 解题报告: 传送门! umm首先提取下题意趴QAQ 大概是说给一棵树,每个点有一个权值,要求修改一些点的权值,使得同一个父亲的儿子权值相同,且父亲的权值必须是所有儿子权值之 ...

  3. 洛谷P1117 优秀的拆分【Hash】【字符串】【二分】【好难不会】

    题目描述 如果一个字符串可以被拆分为AABBAABB的形式,其中 A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串aabaabaaaabaabaa,如果令 A=aabA ...

  4. 洛谷 P3539 [POI2012]ROZ-Fibonacci Representation 解题报告

    P3539 [POI2012]ROZ-Fibonacci Representation 题意:给一个数,问最少可以用几个斐波那契数加加减减凑出来 多组数据10 数据范围1e17 第一次瞬间yy出做法, ...

  5. 洛谷P3533 [POI2012]RAN-Rendezvous

    P3533 [POI2012]RAN-Rendezvous 题目描述 Byteasar is a ranger who works in the Arrow Cave - a famous rende ...

  6. 洛谷P3531 [POI2012]LIT-Letters

    题目描述 Little Johnny has a very long surname. Yet he is not the only such person in his milieu. As it ...

  7. BZOJ2801/洛谷P3544 [POI2012]BEZ-Minimalist Security(题目性质发掘+图的遍历+解不等式组)

    题面戳这 化下题面给的式子: \(z_u+z_v=p_u+p_v-b_{u,v}\) 发现\(p_u+p_v-b_{u,v}\)是确定的,所以只要确定了一个点\(i\)的权值\(x_i\),和它在同一 ...

  8. 洛谷P3539 [POI2012] ROZ-Fibonacci Representation

    题目传送门 转载自:five20,转载请注明出处 本来看到这题,蒟蒻是真心没有把握的,还是five20大佬巨orz 首先由于斐波拉契数的前两项是1,1 ,所以易得对于任何整数必能写成多个斐波拉契数加减 ...

  9. 洛谷P3537 [POI2012]SZA-Cloakroom(背包)

    传送门 蠢了……还以为背包只能用来维护方案数呢……没想到背包这么神奇…… 我们用$dp[i]$表示当$c$的和为$i$时,所有的方案中使得最小的$b$最大时最小的$b$是多少 然后把所有的点按照$a$ ...

随机推荐

  1. docker插件

    import docker c = docker.Client(base_url='unix://var/run/docker.sock',version='1.15',timeout=10) pri ...

  2. 洛谷 2777 [AHOI2016初中组]自行车比赛

    [题解] 为了让某个选手能够获得总分第一,就让他最后一天的得分是n,并且让别的选手的得分的最大值尽量小.于是我们先把目前积分排序,并且让他们最后一天的排名刚好与积分排名相反.即某个积分排名为X的人最后 ...

  3. mysql 5.5与5.6 timestamp 字段 DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP的区别

    http://www.111cn.net/database/mysql/55392.htm 本文章来给各位同学介绍关于mysql 5.5与5.6 timestamp 字段 DEFAULT CURREN ...

  4. Python学习笔记 (2.2)Python中的字符编码问题及标准数据类型之String(字符串)

    Python3中的String类型 首先,Python中没有字符类型,只有字符串类型.单个字符按照长度为1的字符串处理,这对于曾是OIER的我来说有点不适应啊. 字符串的表示方法 最常用的就是用一对双 ...

  5. Codeforces Round #240 (Div. 2) C Mashmokh and Numbers

    , a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge ...

  6. 【整理】uclibc,eglibc,glibc之间的区别和联系

    http://www.crifan.com/relation_between_uclibc_glibc_eglibc/ 1.Glibc glibc = GNU C Library 是GNU项(GNU ...

  7. Ubuntu 16.04安装Chrome浏览器

    一.先有一个hosts能访问Google 参考:http://www.cnblogs.com/EasonJim/p/5999060.html 二.安装方法有两种,如下所示: 1.下载deb包(推荐) ...

  8. 两张图让git新手在项目中运用git命令行

    创建分支命令:    git branch (branchname) 切换分支命令:      git checkout (branchname) 当你切换分支的时候,Git 会用该分支的最后提交的快 ...

  9. 关于jQuery的append()和prepend()方法的小技巧

    最近工作上有个需求是要求一个自动向上滚动的列表,表有很多行,但只显示一行,每次滚动一行.很简单的一个功能,代码如下 <div class="scroll-area"> ...

  10. Lua的面向对象程序设计

    Account={balance=} function Account.withdraw(self,v) self.balance=self.balance-v end a={balance=,wit ...