Spoj_704
This topic can be done using the AC automatic mechanism (in fact, it should be called the trie diagram) + dp.
The problem can be equivalent to picking out some characters from the string X to form a new string, and the new string cannot contain y. We can simulate the process of generating a string to walk on the trie graph. There are two options for step I, or go to the X [I] character, either stay in the same place, where stay in the same place is equivalent to removing the X [I] character, so the price should be increased by 1. To retain the minimum cost of the step I to the Point J, you can use F [I] [J] to record it. Of course, the space can be optimized to one-dimensional.
Finally, we will calculate the minimum cost of arriving at each node after walking step Len [X] and output the minimum value.
# Include <stdio. h>
# Include < String . H>
# Define Maxx10010
# Define Maxd 1010
# Define INF 0x3f3f3f
Int Node, next [maxd] [ 128 ], Safe [maxd], Q [maxd], p [maxd], wa [maxd], WB [maxd], * F, * g;
Char BX [Maxx], by [maxd];
Void Newnode ( Int Cur, Int K)
{
++ Node;
Safe [node] = 1 ;
Memset (next [node], 0 , Sizeof (Next [node]);
Next [cur] [k] = node;
}
Void Build ()
{
Int I, J, K, cur, front, rear;
Front = rear = 0 ;
Safe [ 0 ] = 1 , P [ 0 ] = 0 ;
Q [rear ++] = 0 ;
While (Front <rear)
{
Cur = Q [Front ++];
For (I = 0 ; I < 128 ; I ++)
{
If (K = next [cur] [I])
{
Q [rear ++] = K;
If (Cur = 0 )
P [k] = 0 ;
Else
P [k] = next [p [cur] [I];
If (! Safe [p [k])
Safe [k] = 0 ;
}
Else
Next [cur] [I] = next [p [cur] [I];
}
}
}
Void Init ()
{
Int I, J, K, cur;
Cur = node =0 ;
Memset (next [ 0 ], 0 , Sizeof (Next [ 0 ]);
For (I = 0 ; By [I]; I ++)
{
K = by [I];
If (! Next [cur] [k])
Newnode (cur, k );
Cur = next [cur] [k];
}
Safe [cur] =0 ;
Build ();
}
Void Solve ()
{
Int I, J, K, ANS = inf, * t;
F = wa, G = WB;
Memset (F, 0x3f , Sizeof (F [ 0 ]) * Node );
F [ 0 ] = 0 ;
For (I =0 ; BX [I]; I ++)
{
Memset (G, 0x3f , Sizeof (G [ 0 ]) * Node );
For (J = 0 ; J <node; j ++)
{
If (F [J] + 1 <G [J])
G [J] = f [J] + 1 ;
If (Safe [k = next [J] [BX [I] & F [J] <G [k])
G [k] = f [J];
}
T = F, F = G, G = T;
}
For (I = 0 ; I <node; I ++)
If (F [I] <ans)
Ans = f [I];
Printf ( " % D \ n " , ANS );
}
Int Main ()
{
While (Scanf ( " % S % s " , BX, by) = 2 )
{
Init ();
Solve ();
}
Return 0 ;
}