D. Fixed Prefix Permutations(字典树)

Problem - D - Codeforces

已知n个长度为m的排列a1, a2,, an。回想一下,长度为m的排列是由m个从1到m的不同整数组成的序列。假设排列P1,P2Pm是最大的k,使得P1, p2=2,,Pk=k。如果是p1,那么美是0。两个排列p·q的乘积是一个排列r,使得r3=对于从1到n的每一个i,输出从1到n的所有j(可能,i = j)的a - aj排列的最大美。输入第一行包含一个整数t (1 < t < 104)——测试用例的数量。每个测试用例的第一行包含两个整数n和m (1 <n <5-104;1 < m < 10) -排列的数目和每个排列的长度。接下来n行的第i行包含一个排列ai -m个从1到m的不同整数。在所有的测试用例中,n的和不超过5-104。输出对于每个测试用例,打印n个整数。第i个值应该等于排列a的最大美;- aj除以j (1 <j S n)。 

Example

input

Copy

 

3

3 4

2 4 1 3

1 2 4 3

2 1 3 4

2 2

1 2

2 1

8 10

3 4 9 6 10 2 7 8 1 5

3 9 1 8 5 7 4 10 2 6

3 10 1 7 5 9 6 4 2 8

1 2 3 4 8 6 10 7 9 5

1 2 3 4 10 6 8 5 7 9

9 6 1 2 10 4 7 8 3 5

7 9 3 2 5 6 4 8 1 10

9 4 3 7 5 6 1 10 8 2

output

Copy

1 4 4 
2 2 
10 8 1 6 8 10 1 7 t

题解:

说实话,完全看不出来是字典树,为啥用字典树比较难用文字解释

写个例子应该会比较好理解

3 4 2 1 是其中一个排列

得到一个pos数组

p[3] = 1

p[4] = 2

p[2] = 3

p[1] = 4

我们按下标为1~4的顺序把4 3 1  2存进字典树

我们就会惊奇的发现,如果询问时,存在4 3 1 2的序列,刚好是我们要找的最优的序列p

也就是在询问中,找到最长匹配的

本题完

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<cstdio>
using namespace std;
//#define int long long
const int N = 2e6 + 10;
typedef pair<int, int> PII;
typedef long long ll;
int n,m;
int tre[500004][20];
int a[500006][20];
int pos[20];
void solve() 
{
	cin >> n >> m;
	int idx = 0;
	for(int i = 0;i <= n*m;i++)
	{
		memset(tre[i],0,sizeof(tre[i]));
	}
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= m;j++)
		{
			scanf("%d",&a[i][j]);
			pos[a[i][j]] = j;
		}
		int p = 0;
		for(int j = 1;j <= m;j++)
		{
			if(!tre[p][pos[j]])
			{
				tre[p][pos[j]] = ++ idx;
				p = tre[p][pos[j]];
 			}
 			else
 			p = tre[p][pos[j]];
		}
	}
	for(int i = 1;i <= n;i++)
	{
		int ans = 0,p = 0;
		for(int j = 1;j <= m;j++)
		{
			if(tre[p][a[i][j]])
			{
				ans ++;
				p = tre[p][a[i][j]];
			}
			else
			break;
		}
		printf("%d ",ans);
	}
	printf("\n");
}

//1 2 4
signed main() 
{
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	int t = 1;
//	cin >> t;
scanf("%d",&t);
	while (t--) 
	{
		solve();
	}
}
//1 1 1 0 1

//1 1 1 0 1
//1 1 1 0 1
//1 1 1 0 1
//0 1 1 1 1
//0 1 1 1 1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值