Let's maximize \ (Val = \ sqrt [k] {\ prod {i = 1} ^ k w} \), where \ (K \) is the number of spells and \ (W} \) is the magic power of the \ (i \) first spell.
Looking at the root sign and tired riding, we took \ (\ ln \) from both sides.
\[\ln val=\frac{1}{k}\sum_{i=1}^k \ln w_i\]
It is easy to know that when \ (\ ln val \) is maximized, \ (Val \) is also maximized. So we turned the problem into maximization \ (\ frac {1} {K} \ sum {I = 1} ^ k \ ln w \ We can find its maximum value through the binary answer. The question is how to check whether the binary answer is legal:
Legal only when \ (\ frac {1} {K} \ sum {I = 1} ^ k \ ln w {I > mid \)
That is, it is only valid when \ (\ sum {I = 1} ^ k (w {i-mid) > 0 \)
First, we build AC automata for all spells, run dp to find the maximum value of \ (\ sum {I = 1} ^ k (w {i-mid)), and judge whether it is feasible.
Specific dp: just like many other dp on AC automata, set \ (f[i][j] \) to represent \ (I \) characters in front of the staff, match to \ (j \) node on AC automata, and transfer according to the routine, that is, don't forget the original limitation of the topic.
The accuracy error of this algorithm is large, but the problem is still passed.
#include <bits/stdc++.h>
#define db double
#define N 1505
#define eps 1e-6
using namespace std;
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
int n,m;
char T[N],s[N],ans[N];
db V[N],val[N],f[N][N];
pair<int,int> pre[N][N];
struct node{
int son[10],fail,cnt;
db val;
}tr[N];
int tot=0;
inline void Insert(register char *s,register db v)
{
int len=strlen(s+1),now=0;
for(register int i=1;i<=len;++i)
{
if(!tr[now].son[s[i]-'0'])
tr[now].son[s[i]-'0']=++tot;
now=tr[now].son[s[i]-'0'];
}
++tr[now].cnt,tr[now].val+=v;
}
inline void getfail()
{
queue<int> q;
for(register int i=0;i<10;++i)
if(tr[0].son[i])
q.push(tr[0].son[i]);
while(!q.empty())
{
int u=q.front();
q.pop();
tr[u].cnt+=tr[tr[u].fail].cnt;
tr[u].val+=tr[tr[u].fail].val;
for(register int i=0;i<10;++i)
{
if(tr[u].son[i])
{
tr[tr[u].son[i]].fail=tr[tr[u].fail].son[i];
q.push(tr[u].son[i]);
}
else
tr[u].son[i]=tr[tr[u].fail].son[i];
}
}
}
inline void updateans(register int i,register int j)
{
if(!i)
return;
updateans(i-1,pre[i][j].first);
ans[i]=pre[i][j].second+'0';
}
inline bool check(register db mid)
{
memset(f,-0x3f,sizeof(f));
db inf=-f[0][0];
f[0][0]=0;
for(register int i=1;i<=n;++i)
for(register int j=0;j<=tot;++j)
{
if(fabs(f[i-1][j]+inf)<1)
continue;
if(T[i]=='.')
{
for(register int k=0;k<10;++k)
{
int v=tr[j].son[k];
if(f[i-1][j]+tr[v].val-tr[v].cnt*mid>f[i][v])
{
f[i][v]=f[i-1][j]+tr[v].val-tr[v].cnt*mid;
pre[i][v]=make_pair(j,k);
}
}
}
else
{
int k=T[i]-'0',v=tr[j].son[k];
if(f[i-1][j]+tr[v].val-tr[v].cnt*mid>f[i][v])
{
f[i][v]=f[i-1][j]+tr[v].val-tr[v].cnt*mid;
pre[i][v]=make_pair(j,k);
}
}
}
int pos=0;
for(register int i=1;i<=tot;++i)
if(f[n][i]>f[n][pos])
pos=i;
if(f[n][pos]>eps)
{
updateans(n,pos);
return 1;
}
else
return 0;
}
int main()
{
n=read(),m=read();
scanf("%s",T+1);
db L=0,R=0;
for(register int i=1;i<=m;++i)
{
scanf("%s",s+1);
V[i]=log(read());
R=max(R,V[i]);
Insert(s,V[i]);
}
getfail();
while(R-L>eps)
{
db mid=(L+R)/2.0;
if(check(mid))
L=mid;
else
R=mid;
}
for(register int i=1;i<=n;++i)
putchar(ans[i]);
return 0;
}