概述
AC自动机+状态压缩递推,通过这题主要学习了一下输出答案的方法,DP的话输出状态比较好办,因为最优决策一般只是一条路径,所以记录一下前驱可以用线性的复杂度输出路径,而递推确是多条路径的合集,输出路径比较麻烦,需要根据最终状态向前递推,并把经过的路径节点全部标记出来,由于递推状态设计本身是不会有重复的路径的,所以最后再从头dfs一遍访问过的节点并且记录,当递归深度合适时,输出记录即可。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <ctime>
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
const int MAXN(110);
const int SIGMA_SIZE(26);
const int MAXM(110);
const int MAXE(4000010);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 2);
const int MOD(10007);
const ULL LIM(1000000000000000ull);
int point[12];
int que[MAXN];
int front, back;
struct AC
{
int ch[MAXN][SIGMA_SIZE];
int f[MAXN];
int val[MAXN];
int size;
void init()
{
memset(ch[0], 0, sizeof(ch[0]));
f[0] = val[0] = 0;
size = 1;
}
inline int idx(char temp)
{
return temp-'a';
}
void insert(char *S, int tv)
{
int u = 0, id;
for(; *S; ++S)
{
id = idx(*S);
if(!ch[u][id])
{
memset(ch[size], 0, sizeof(ch[size]));
val[size] = 0;
ch[u][id] = size++;
}
u = ch[u][id];
}
val[u] |= (1 << tv);
}
void construct()
{
front = back = 0;
int cur, u;
for(int i = 0; i < SIGMA_SIZE; ++i)
{
u = ch[0][i];
if(u)
{
que[back++] = u;
f[u] = 0;
}
}
while(front < back)
{
cur = que[front++];
for(int i = 0; i < SIGMA_SIZE; ++i)
{
u = ch[cur][i];
if(u)
{
que[back++] = u;
f[u] = ch[f[cur]][i];
val[u] |= val[f[u]];
}
else
ch[cur][i] = ch[f[cur]][i];
}
}
}
};
AC ac;
LL table[26][MAXN][1 << 10];
bool vis[26][MAXN][1 << 10];
char rec[27];
int n;
void dfs(int dep, int cur, int state)
{
if(dep == n)
{
rec[dep] = '