T6 要有光题解
作者:@cqbz_llsw 。
阅读题面,首先便不难发现一点,一旦进行了除了 光弋 操作外的任一操作,这个法术都会变成一个归法术。而 光弋 只能在法术前端加入字符,又只能放在最后做,所以可以得出:
除特殊情况外,我们在最终 光弋 操作之前的目标就是要让初始法术通过最少时间变换为目标法术的一个归法术后缀;最终不需要进行 光弋 操作的情况也可以归为这类。
特殊情况就是当目标法术就是初始法术的情况。此时我们不需要变换。
现在的问题是:如何用最少时间变换为其一个归法术后缀?
再次观察我们剩下的四种操作,我们不难联想到另一个东西:
回文自动机!
可以发现题目中 光归 和 光辉 操作实际上是对应了状态通过
有了这个载体,我们把字符串问题转化为了图上(树上?)问题。此时不排除有我想不出来但是是正确的贪心/ DP 等做法,欢迎大家来踩爆我。
反正,由于是图上最优化问题,并且牵涉到状态的转移,我们不难想到构建最短路。考虑根据题目的前四种操作基于回文自动机建图:
首先定义
然后可以分类建图:
- 光归 :建一条
(x,fail_x) 的有向边,权值为A ; - 光辉 :建一条
(fail_x,x) 的有向边,权值为B ; - 光隐 :建
k 条边,为1\leq i\leq k, (x,fa_{x,i}) ,权值各为C ; - 光腾 :这个建边会直接影响到你的复杂度,下面分析:
考虑最后的 光腾 操作的建边,如果直接暴力无脑向自己在回文自动机上所有子孙都连边显然是不可能的,很容易就会被卡到
也就是说,我们可以假想为每一个点
- 从 实树 走到 虚树 上,花费
D ,即(x,x') ,权值为D 。 - 在 虚树 上向儿子方向转移,不花费。
- 从 虚树 回到 实树 上,不花费,即
(x',x) ,权值为0 。
于是这样建边,光腾 的复杂度就被降到了
接着,我们从最后一个节点开始跑单源最短路,就可以愉快地得到从初始法术到每一个归法术的最少时间花费了!
大体问题解决,需要处理一些小细节。
由于我们需要转移到询问法术的一个归法术后缀,所以我们至少需要知道询问法术对应的 最长归法术后缀 对应的状态。
设
由于
下一个问题。我们之前一直在强调,我们需要走到的状态是目标法术的一个 归法术后缀。为什么不是最长的呢?请看下面这个例子:
acbabcb
2
1 10 10 2 1
1
2 5
如果走到最长的归法术后缀
这是由于 光弋 操作不在我们的图的转移上面导致的。因此我们只需要单独提出来做一下转移就好了。根据上面的例子我们可以知道这个更新方向是
设
设
最后一个小细节,如果初始法术不是一个归法术,那么我们从
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 5, MAXE = 1.5e6 + 5, MAXLOG = 20;
template<typename _T>
void read( _T &x )
{
x = 0;char s = getchar();int f = 1;
while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();}
while( s >= '0' && s <= '9' ){x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar();}
x *= f;
}
template<typename _T>
void write( _T x )
{
if( x < 0 ){ putchar( '-' ); x = ( ~ x ) + 1; }
if( 9 < x ){ write( x / 10 ); }
putchar( x % 10 + '0' );
}
template<typename _T>
_T MIN( const _T a, const _T b )
{
return a < b ? a : b;
}
template<typename _T>
_T MAX( const _T a, const _T b )
{
return a > b ? a : b;
}
struct edge
{
int to, nxt, w;
}Graph[MAXE << 1];
struct node
{
LL dist; int u;
node() {}
node( const int U, const LL D ) { u = U, dist = D; }
bool operator < ( const node & b ) const { return ! ( dist < b.dist ); }
};
priority_queue<node> q;
int fa[MAXN][MAXLOG] = {};
LL head[MAXN * 2], dist[MAXN * 2], f[MAXN * 2];
int ch[MAXN][55], fail[MAXN], len[MAXN], fath[MAXN];
char S[MAXN];
int last[MAXN];
int N, M, A, B, C, D, E, K, lg2, cnt, tot, lst;
bool visited[MAXN * 2];
int HASH( const int x, const int l ) { return x + ( l - 1 ) * tot; }
int id( const char c ) { return 'A' <= c && c <= 'Z' ? ( c - 'A' ) : ( c - 'a' + 26 ); }
void addEdge( const int from, const int to, const int W )
{
Graph[++ cnt].to = to, Graph[cnt].nxt = head[from], Graph[cnt].w = W;
head[from] = cnt;
}
void addE1( const int from, const int to, const int W ) { addEdge( from, to, W ), addEdge( to, from, 0 ); }
void build( const char *s )
{
int x, p, cur;
N = strlen( s + 1 );
fail[0] = ++ tot, len[1] = -1;
for( int i = 1 ; i <= N ; i ++ )
{
x = id( S[i] );
while( S[i] ^ S[i - len[lst] - 1] ) lst = fail[lst];
if( ! ch[lst][x] )
{
len[cur = ++ tot] = len[lst] + 2, p = fail[lst];
while( S[i] ^ S[i - len[p] - 1] ) p = fail[p];
fail[cur] = ch[p][x], ch[lst][x] = cur;
fath[cur] = lst;
}
last[i] = lst = ch[lst][x];
}
}
void init()
{
for( int i = 2, v ; i <= tot ; i ++ )
{
if( fail[i] > 1 ) addEdge( i, fail[i], A ), addEdge( fail[i], i, B );
else addEdge( i, 1, A ), addEdge( 1, i, B );
addE1( HASH( i, 1 ), HASH( i, 2 ), D );
for( int j = 1, k = fath[i] ; j <= K && k > 1 ; j ++, k = fath[k] )
addEdge( i, k, C );
for( int j = 0 ; j < 52 ; j ++ )
if( v = ch[i][j] )
addEdge( HASH( i, 2 ), HASH( v, 2 ), 0 );
}
lg2 = log2( N );
for( int i = 2 ; i <= tot ; i ++ ) fa[i][0] = MAX( fail[i], 1 );
for( int j = 1 ; j <= lg2 ; j ++ )
for( int i = 2 ; i <= tot ; i ++ )
fa[i][j] = fa[fa[i][j - 1]][j - 1];
}
void Dijkstra( const int S )
{
while( ! q.empty() ) q.pop();
for( int i = 0 ; i <= tot * 2 ; i ++ ) dist[i] = INF;
q.push( node( S, dist[S] = 0 ) );
int u, v, w;
while( ! q.empty() )
{
u = q.top().u; q.pop();
if( visited[u] ) continue; visited[u] = true;
for( int i = head[u] ; i ; i = Graph[i].nxt )
if( ! visited[v = Graph[i].to] && dist[v] > dist[u] + ( w = Graph[i].w ) )
q.push( node( v, dist[v] = dist[u] + w ) );
}
}
int find( int p, const int l )
{
if( len[p] <= l ) return p;
for( int j = lg2 ; ~ j ; j -- )
if( fa[p][j] > 1 && len[fa[p][j]] > l )
p = fa[p][j];
return fa[p][0];
}
bool palidrome()
{
for( int i = 1 ; i <= N ; i ++ ) if( S[i] ^ S[N - i + 1] ) return false;
return true;
}
int main()
{
int Q;
scanf( "%s", S + 1 );
read( K );
read( A ), read( B ), read( C ), read( D ), read( E );
build( S );
init();
Dijkstra( lst );
f[0] = INF, f[1] = dist[1];
for( int i = 2 ; i <= tot ; i ++ )
f[i] = MIN( dist[i], f[fail[i]] + 1ll * ( len[i] - len[fail[i]] ) * E );
read( Q );
bool flag = palidrome();
int l, r;
while( Q -- )
{
read( l ), read( r );
if( l == 1 && r == N ) { puts( "0" ); continue; }
int p = find( last[r], r - l + 1 );
write( f[p] + 1ll * ( r - l + 1 - len[p] ) * E + ( ! flag ) * A ), putchar( '\n' );
}
return 0;
}