PAT《团体设计天梯赛》2021

Problem L3-029 还原文件 (30 分)


应 该 算 是 一 道 模 板 题 目 了 , 字 符 串 h a s h + d f s 匹 配 应该算是一道模板题目了,字符串hash+dfs匹配 hash+dfs
建议背下来

/*Love coding and thinking!*/ 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back 
#define mem(f, x) memset(f,x,sizeof(f)) 
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef pair<int,int>PII;
typedef pair<long,long>PLL;

typedef long long ll;
typedef unsigned long long ull; 
const int N=1e5+10,M=110,P=131;
int n,m,_;

ull g[N],p[N];//长版的hash
ull Hash[M];//每个小长条的hash
int width[M];
int ans[M];
bool st[M];//判重数组
int x;
ull get(int l,int r)//返回区间hash值
{
	return g[r]-g[l-1]*p[r-l+1];
}

bool dfs(int u,int ed)//第u个长条能不能拼接上,上一个的结尾是ed
{
	if(ed==n)
	{
		return true;	
	}
	
	for(int i=1;i<=m;i++)
	{
		if(!st[i]&&Hash[i]==get(ed,ed+width[i]-1))
		{
			st[i]=1;
			ans[u]=i;
			if(dfs(u+1,ed+width[i]-1))
				return 1;	
			st[i]=0;
		}
	}
	return false;
}
int main()
{
	cin>>n;
	p[0]=1;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		p[i]=p[i-1]*P;//只在得到区间hash值时用到
		g[i]=g[i-1]*P+x+1;//一般都是*P,注意区分,因为0、00的会重复,所以从1开始
	}
	cin>>m;
	
	for(int i=1;i<=m;i++)
	{
		scanf("%d",&width[i]);
		for(int j=0;j<width[i];j++)
		{
			scanf("%d",&x);
			Hash[i]=Hash[i]*P+x+1;
		}
	}
	dfs(1,1);
	for(int i=1;i<=m;i++)
		if(i!=m)
			printf("%d",ans[i]);
		else
			printf("%d ",ans[i]);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值