L3-029 还原文件 (30 分)(哈希+dfs)

不知道正解是啥。

暴力dfs过了。我爬了

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
const LL base=131;
const LL mod=998244353;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL hashv[110],len[110];
bool vis[110];
LL hashp[maxn],a[maxn];
LL p[maxn];
vector<LL>ans;
bool flag=0;
LL n,m;
LL gethash(LL l,LL r){
   return  (hashp[r]%mod-hashp[l-1]*p[r-l+1]%mod+mod)%mod;
}
void dfs(LL id,LL pos,LL res){///当前的串号,当前需要匹配到的头位置,还需要匹配的串的个数
     if(flag) return;
     if(res<=1){
        bool ok=1;
        for(LL i=1;i<=m;i++){
            if(!vis[i]) ok=0;
        }
        if(ok){
            flag=1;return;
        }
     }
     for(LL i=1;i<=m;i++){
        if(vis[i]) continue;
        if(gethash(pos+len[id]-1,pos+len[id]-1+len[i]-1)==hashv[i]){
              vis[i]=true;
              ans.push_back(i);
              dfs(i,pos+len[id]-1,res-1);
              if(flag) return;
              ans.pop_back();
              vis[i]=false;
         }
      }
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   cin>>n;
   p[0]=1;
   for(LL i=1;i<maxn;i++) p[i]=p[i-1]*base%mod;
   for(LL i=1;i<=n;i++){
      cin>>a[i];
      hashp[i]=(hashp[i-1]*base%mod+(a[i]-0)%mod)%mod;
   }
   cin>>m;
   for(LL i=1;i<=m;i++){
       LL k;cin>>k;
       LL num=0;
       for(LL j=1;j<=k;j++){
           LL x;cin>>x;
           num=(num*base%mod+(x-0)%mod)%mod;
       }
       hashv[i]=num;
       len[i]=k;
   }
   for(LL i=1;i<=m;i++){
      if(hashv[i]==gethash(1,len[i])){
         memset(vis,0,sizeof(vis));
         ans.push_back(i);
         vis[i]=true;
         dfs(i,1,m);
         if(flag==1){
            for(LL j=0;j<ans.size()-1;j++){
                cout<<ans[j]<<" ";
            }
            cout<<(*(ans.end()-1))<<"\n";
            break;
         }
         else ans.clear();
      }
   }
   return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值