题目
题意: 略。
思路: 用字符串哈希优化dfs。
时间复杂度: O(能过)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 1e5+10;
const int P = 131;
int a[N];
vector<int> va[102],v,res;
bool vis[N];
bool flag = 0;
int n,m,k,T;
ull p[N],h[N],f[N];
ull get(int l,int r)
{
return h[r] - h[l-1]*p[r-l+1];
}
bool dfs(int cur,int now)
{
// cout<<cur<<" "<<now<<endl;
if(cur == m)
{
if(now == n)
{
res = v;
return 1;
}
}
else
{
for(int i=0;i<m;++i)
{
if(vis[i]) continue;
if(f[i] == get(now,now+va[i].size()-1))
{
v.push_back(i);
vis[i] = 1;
if(dfs(cur+1,now+va[i].size()-1))return 1;
vis[i] = 0;
v.pop_back();
}
}
}
return 0;
}
void solve()
{
cin>>n;
p[0] = 1;
for(int i=1;i<=n;++i) cin>>a[i],p[i] = p[i-1]*P,h[i] = h[i-1]*P+a[i];
// for(int i=1;i<=n;++i) cout<<i<<":"<<p[i]<<" "<<h[i]<<endl;
cin>>m;
for(int i=0;i<m;++i)
{
int num; cin>>num;
while(num--)
{
int x; cin>>x;
va[i].push_back(x);
f[i] = f[i]*P+x;
}
}
dfs(0,1);
for(int i=0;i<res.size();++i)
{
if(i) cout<<' ';
cout<<res[i]+1;
}
}
signed main(void)
{
solve();
return 0;
}