ans[i][j]表示在i节点,走j步,正好停留在有单词的节点的概率。
对于有单词的节点p, ans[p][0]=1, ans[p][other]=0
然后直接暴力概率转移即可。 写的不优美。。网上看搜了别人的程序,思想大致相同…… 但是大家的程序优美一些
这次AV自动机,得按照LRJ说的那样改一下ch数组,直接实现失配指针的链接。这样后面DP好写一些。
模板
struct AhoCorasickAutomata {
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE]; // fail函数
int val[MAXNODE]; // 每个字符串的结尾结点都有一个非0的val
int last[MAXNODE]; // 输出链表的下一个结点
double ans[MAXNODE][104];
int sz;
queue<int>q;
void init() {
sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
}
// 字符c的编号
int idx(char c)
{
//if (c == '\0') return 62;
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
return c - 'A' + 36;
}
// 插入字符串。v必须非0
void insert(char s[], int len, int v) {
int now = 0;
for(int i = 0; i < len; i++) {
int c = idx(s[i]);
if(!ch[now][c]) {
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[now][c] = sz++;
}
now = ch[now][c];
}
val[now] ++;//单词出现次数
//ms[string(s)] = v;
}
// 递归打印以结点j结尾的所有字符串
void print(int j) {
if(j) {
//cnt[val[j]]++;
//cnt[j]=1;
print(last[j]);
}
}
// 在T中找模板,text串的下标从0开始,长度为len
void find(char text[], int len) {
int j = 0; // 当前结点编号,初始为根结点
for(int i = 0; i < len; i++) { // 文本串当前指针
int c = idx(text[i]);
//while(j && !ch[j][c]) j = f[j]; // 顺着细边走,直到可以匹配
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]); // 找到了!
}
}
//计算fail指针
void get_fail()
{
f[0] = 0;//fail[i]表示,当匹配到某个位置失败,下一个自动的位置
for (int c = 0; c < SIGMA_SIZE; c++)
{
int will = ch[0][c];
if (will)
{
f[will]=0;
q.push(will);
last[will] = 0;
}
}
while (!q.empty())
{
int now = q.front();
q.pop();
for (int c = 0; c < SIGMA_SIZE; ++ c)
{
int will = ch[now][c]; //now节点,想要访问的下标
// if (!will) continue;//如果这个下标不存在,直接continue;
if (!will)
{
ch[now][c] = ch[f[now]][c];
continue;
}
q.push(will);
int pre = f[now]; //失配指针,先指now的失配,至少有一段都是相等的
while (pre && !ch[pre][c]) pre = f[pre];//往前跳失配指针,类似 KMP
f[will] = ch[pre][c]; //f为成功指针,指向应该的方向
last[will] = val[f[will]] ? f[will] : last[f[will]];
}
}
}
}ac;
AC CODE
#include<cstring>
#include <cmath>
#include <iostream>
#include<queue>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
const int SIGMA_SIZE = 63;
const int MAXNODE = 1000;
#define prln(x) cout<<#x<<" = "<<x<<endl
#define pr(x) cout<<#x<<" = "<<x<<" "
char text[1000031];
char tmp[100];
int n, m, L;
double gailv[65];
/*
* AC自动机,令g[i,j]表示从i到j这一路遍历的所有字符串。 f[i]的意义就是g[?,i]和g[0,f[i]]的字符串是相等的
* last[i] ,表示g[0,last[i]]的字符串,是确定存在的,并且以last[i]结尾的字符串*/
struct AhoCorasickAutomata {
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE]; // fail函数
int val[MAXNODE]; // 每个字符串的结尾结点都有一个非0的val
int last[MAXNODE]; // 输出链表的下一个结点
double ans[MAXNODE][104];
int sz;
queue<int>q;
void init() {
sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
}
// 字符c的编号
int idx(char c)
{
//if (c == '\0') return 62;
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
return c - 'A' + 36;
}
// 插入字符串。v必须非0
void insert(char s[], int len, int v) {
int now = 0;
for(int i = 0; i < len; i++) {
int c = idx(s[i]);
if(!ch[now][c]) {
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[now][c] = sz++;
}
now = ch[now][c];
}
val[now] ++;//单词出现次数
//ms[string(s)] = v;
}
// 递归打印以结点j结尾的所有字符串
void print(int j) {
if(j) {
//cnt[val[j]]++;
//cnt[j]=1;
print(last[j]);
}
}
// 在T中找模板,text串的下标从0开始,长度为len
void find(char text[], int len) {
int j = 0; // 当前结点编号,初始为根结点
for(int i = 0; i < len; i++) { // 文本串当前指针
int c = idx(text[i]);
//while(j && !ch[j][c]) j = f[j]; // 顺着细边走,直到可以匹配
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]); // 找到了!
}
}
//计算fail指针
void get_fail()
{
f[0] = 0;//fail[i]表示,当匹配到某个位置失败,下一个自动的位置
for (int c = 0; c < SIGMA_SIZE; c++)
{
int will = ch[0][c];
if (will)
{
f[will]=0;
q.push(will);
last[will] = 0;
}
}
while (!q.empty())
{
int now = q.front();
q.pop();
for (int c = 0; c < SIGMA_SIZE; ++ c)
{
int will = ch[now][c]; //now节点,想要访问的下标
// if (!will) continue;//如果这个下标不存在,直接continue;
if (!will)
{
ch[now][c] = ch[f[now]][c];
continue;
}
q.push(will);
int pre = f[now]; //失配指针,先指now的失配,至少有一段都是相等的
while (pre && !ch[pre][c]) pre = f[pre];//往前跳失配指针,类似 KMP
f[will] = ch[pre][c]; //f为成功指针,指向应该的方向
last[will] = val[f[will]] ? f[will] : last[f[will]];
}
}
}
void doit()
{
/*
// * 调试ac自动机数组信息用的
for (int i = 0; i < sz; ++ i)
{
prln(i);
for (int j = 0; j < 26;++j)
{
if (ch[i][j]) cout<< ((char)(j+'a'))<<" "<<ch[i][j]<<endl;;
}
//cout<<endl;
//cout<<endl;
}
for (int i = 0; i < sz; ++ i)
{
pr(i),prln(val[i]);
}
for (int i = 0; i < sz; ++ i)
{
pr(i),prln(f[i]);
}
for (int i = 0; i < sz; ++ i)
{
pr(i),prln(last[i]);
}
*/
memset(ans, 0, sizeof(ans));
int fg[1000]={0};
for (int i = 0; i < sz; ++ i)
{
if (val[i])
{
ans[i][0] = 1;
fg[i]=1;
}
int p=last[i];
if (p)
{
fg[i] = 1;
ans[i][0] = 1;
}
while (p)
{
fg[p]=1;
ans[p][0] = 1;
p=last[p];
}
}
for (int i = 1; i <= L; ++ i)
{
for (int j = 0; j < sz; ++ j)
{
if (fg[j]) continue;
for (int k = 0; k < 63; ++ k)
{
ans[j][i] += ans[ch[j][k]][i - 1] * gailv[k];
}
}
}
double ret=0;
for (int i = 0; i <= L;++i)
ret+=ans[0][i];
printf("%.6lf\n", fabs(1-ret));
}
}ac;
int main() {
int T;
scanf("%d", &T);
int sb=0;
while (T--)
{
ac.init();
scanf("%d", &n);
memset(gailv,0,sizeof(gailv));
for(int i = 1; i <= n; i++) {
scanf("%s", tmp);
//cout<<tmp<<endl;
ac.insert(tmp, strlen(tmp), i);
}
ac.get_fail();
scanf("%d", &m);
for (int i = 1; i <= m ; ++ i)
{
char ch;
double x;
scanf(" %c %lf", &ch, &x);
gailv[ac.idx(ch)] = x;
}
scanf("%d", &L);
//prln(L);
printf("Case #%d: ",++sb);
ac.doit();
}
return 0;
}
简单的数据生成器:
#include <cstdio>
#include <ctime>
#include <iostream>
#include <cstdlib>
using namespace std;
char get_rand()
{
int flag= rand()%3;
if (flag==0)
{
return rand()%10 + '0';
}
if (flag==1)
{
return rand()%26+'a';
}
return rand()%26+'A';
}
int rand_from_to(int a, int b)
{
return rand()%(b-a+1) + a;
}
int main()
{
int str_len = 4;//串长
int char_num = 3;//字符总数
int n_num = 3;//串数
srand(time(0));
int T=1;
cout<<T<<endl;
while (T--)
{
int n = n_num; //串数
cout<<n<<endl;
for (int i = 1; i <=n;++i)
{
int len = rand()%str_len+1; //串长
for (int j = 1; j <=len;++j)
{
cout<<(char)(rand()%char_num+'a');
}
cout<<endl;
}
cout<<char_num<<endl;
int p = 1000;
for (int i =1;i<=char_num;++i)
{
cout<< (char)('a'+i-1)<<" ";
if (i==char_num)
{
printf("%.6lf\n",p/1000.0);
break;
}
double x = (rand()%1000)/1000.0;
int t=p*x;
//x占p的百分比
printf("%.6lf\n",t/1000.0);
p-=t;
}
int L = rand_from_to(0,100);
cout<<L<<endl;
}
}
/*
1
20
ade
ad
acae
c
cc
c
c
de
ac
c
bd
dce
be
e
bc
eac
ddcc
eda
ae
bdba
5
a 0.633000
b 0.010000
c 0.160000
d 0.152000
e 0.045000
7
1
3
aacb
c
cbb
3
a 0.362000
b 0.546000
c 0.092000
3
*/