Description
Appleman和Toastman在玩游戏。首先Appleman告诉Toastman两个仅包含“A”、“B”、“C”、“D”的两个字符串s和t,要求Toastman构造s。一开始Toastman有个空串,每次他选取t的一个连续子串连接在他的串后面,直到他得到s为止。Appleman当然希望这个构造尽量难,现在他已经想好了t,他需要一个长度为n的s使得Toastman需要连接尽量多次。现在你需要告诉Appleman,通过确定一个s,最多能使Toastman连接多少次。假设Toastman一直采用最优策略。
Input
第一行一个整数n。
第二行一个字符串t。
Output
一个整数:最多能使Toastman连接多少次。
Sample Input
输入1:
5
ABCCAD
输入2:
5
AAABACADBABBBCBDCACBCCCDDDBDCDD
Sample Output
输出1:
5
输出2:
4
[样例解释]
对于第一个样例,s可以为“AAAAA”。
对于第二个样例,s可以为“DADDA”。
Data Constraint
对于20%的数据,n<=10。
对于另外20%的数据,|t|<=100。
对于100%的数据,n<=10^18,|t|<=10^5。
The Solution
这道题其实比赛时我就打出来了,但是那时心里没有底,就没有交了。然后比赛后看了正解好像跟我的想法差不多,但是理解方面有些差异。比赛后跟J枫进行了一波学术交流♂,发现自己的语文表达真是弱到家了QAQ
BB了这么多开始进入正题:
题意大概就是给出一个字符串t,要我们构造一个满足题意的s,并求出使其连接次数最多,当然构造的字符全选自给定字符串t的子串。
语文不好,请见谅
好了,显然我们要求的最优的贡献就是每次选t中尽可能长的子串吧,假设你选了s1,s2那么要保证s1 + s2[1]不属于t中的子串方案才能最优嘛。
那么第一个想法通常是暴力吧,可是看看数据就知道是天方夜谭,不可行,去做梦吧QAQ。
我们可以我们可以对 s 的所有后缀建一棵 伪trie(和后缀树有些区别,因为不同后缀的叶子要保留自己 的深度,不能合并),然后扫描所有叶子,即可找出所有的 s1+s2[1]。
令 a[i][j] 为转移矩阵,表示从字母 i 到字母 j(i 包含在长度内而 j 不含,即 i 为 s1[1], j 为 s2[1])经过一次连接能获得的最短长度,则 a[s1[1]][s2[1]] = min(a[s1[1]][s2[1]], dep-1)。
说得简单点,就是a[i][j]表示以i开头的子串和以j开头的子串
,经过一次连接能获得的最短长度。
然后我考试时通过暴力打表,发现Trie里其实下面有很多状态是不用的,于是用了点淫技。
下面是官方给出的题解证明
进一步我们可以发现,trie 深度大于 log4(|t|+1)+2 的部分是没用的。因为 如果长度为 l 的所有可能串都是 t 的子串,那么必有 4 l >=|t|-l+1,不过因为这 个子串的首尾要确定所以还要加 2。于是我们可以只用 t 后缀的 log 长度的前缀 建树。 预处理出 a 后,可以通过类似矩阵乘法的转移求出连接 2 k次获得的最短长度, 同时我们只需使用倍增即可在 4 3 log2n 的时间内出解,先二分再倍增的 4 3 log2 2 n 算法也能通过数据,官方题解就是这么写的。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#define fo(i,a,b) for (int i=a;i<=b;i++)
#define fd(i,a,b) for (int i=a;i>=b;i--)
#define N 100005
#define INF 1 << 30
using namespace std;
typedef long long ll;
char s[N],t[N];
ll n,Ans = 0,root,tot;
int Id[15];
ll a[5][5][64],Two[64],b[5][5],c[5][5];
int Trie[N * 10][5];
int read(ll &n)
{
char ch = ' ';
ll q = 0, w = 1;
for (;(ch != '-') && ((ch < '0') || (ch> '9'));ch = getchar());
if (ch == '-') w = -1,ch = getchar();
for (; ch >= '0' && ch <= '9';ch = getchar()) q = q * 10 + ch - 48;
n = q * w;
return n;
}
namespace ib {char b[100];}
inline void pint(ll x)
{
if (x == 0)
{
putchar(48);
return;
}
if (x < 0)
{
putchar('-');
x = -x;
}
char *s = ib :: b;
while (x) *(++ s) = x % 10,x /= 10;
while (s != ib :: b) putchar((* (s --)) + 48);
}
void Add(int head,int len)
{
int x = root;
fo(i,head,head + len - 1)
{
int y = s[i] - 64;
if (!Trie[x][y]) Trie[x][y] = ++ tot;
x = Trie[x][y];
}
}
void Dfs(int i,int j,int x,int len)
{
if (!Trie[x][j])
{
a[i][j][0] = min((ll)len,a[i][j][0]);
return;
}
if (len > a[i][j][0]) return;
fo(k,1,4)
if (Trie[x][k]) Dfs(i,j,Trie[x][k],len + 1);
}
int main()
{
freopen("xor4.in","r",stdin);
read(n);
scanf("%s",s + 1);
root = 1,tot = 1;
Id[0] = 1;
int tmp,size = strlen(s + 1);
fo(i,1,size)
{
Id[i] = Id[i - 1] * 4;
if (Id[i] >= size - i + 1)
{
tmp = i;
break;
}
}
fo(i,1,size) Add(i,min(size - i + 1,tmp));
fo(i,1,4)
fo(j,1,4)
if (Trie[root][i] && Trie[root][j]) a[i][j][0] = INF;
fo(i,1,4)
fo(j,1,4)
if (Trie[root][i] && Trie[root][j]) Dfs(i,j,Trie[root][i],1);
Two[0] = 1;
tmp = 0;
while (++ tmp)
{
Two[tmp] = Two[tmp - 1] * 2;
if (Two[tmp] > n) break;
fo(i,1,4)
fo(j,1,4)
fo(k,1,4)
if (Trie[root][i] && Trie[root][j] && Trie[root][k])
if (!a[i][j][tmp] || a[i][k][tmp - 1] + a[k][j][tmp - 1] < a[i][j][tmp])
a[i][j][tmp] = a[i][k][tmp - 1] + a[k][j][tmp - 1];
}
bool bz;
fd(k,tmp - 1,0)
{
ll len = n * 2;
if (!bz)
{
fo(i,1,4)
fo(j,1,4)
if (Trie[root][i] && Trie[root][j]) len = min(len,a[i][j][k]);
if (len < n)
{
bz = true;
fo(i,1,4)
fo(j,1,4) b[i][j] = a[i][j][k];
Ans += Two[k];
}
}
else
{
fo(i,1,4)
fo(j,1,4)
{
c[i][j] = 0;
fo(l,1,4)
if (Trie[root][i] && Trie[root][j] && Trie[root][l])
if (!c[i][j] || b[i][l] + a[l][j][k] < c[i][j]) c[i][j] = b[i][l] + a[l][j][k];
if (Trie[root][i] && Trie[root][j]) len = min(len,c[i][j]);
}
if (len < n) Ans += Two[k],memcpy(b,c,sizeof(b));
}
}
pint(Ans + 1);
return 0;
}