大意: 给定m个n排列, 求有多少个公共子串.

枚举每个位置, hash求出最大匹配长度.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e5+10;
int n, m, lst, s[12][N];
int pos[12][N];
int bas[N], has[12][N];
int Hash(int id, int l, int r) {
int ret = (has[id][r]-(ll)has[id][l-1]*bas[r-l+1])%P;
if (ret<0) ret += P;
return ret;
}
int calc(int x, int p1, int y, int p2) {
int r=min({lst+1,p1,p2});
return Hash(x,p1-r+1,p1)==Hash(y,p2-r+1,p2)?r:1;
}
int main() {
scanf("%d%d", &n, &m);
bas[0] = 1, bas[1] = n*20+1;
REP(i,2,n) bas[i]=(ll)bas[i-1]*bas[1]%P;
REP(i,1,m) REP(j,1,n) {
scanf("%d", s[i]+j);
has[i][j] = ((ll)has[i][j-1]*bas[1]+s[i][j])%P;
pos[i][s[i][j]] = j;
}
if (m==1) return printf("%lld\n",(ll)n*(n+1)/2),0;
ll ans = 0;
REP(i,1,n) {
int d = 1e9;
REP(j,2,m) d = min(d, calc(1,i,j,pos[j][s[1][i]]));
ans += d;
lst = d;
}
printf("%lld\n", ans);
}

Mysterious Crime CodeForces - 1043D (哈希)的更多相关文章

  1. Mysterious Crime CodeForces - 1043D (思维+组合数学)

    Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody ...

  2. CodeForces 1043D Mysterious Crime 区间合并

    题目传送门 题目大意: 给出m个1-n的全排列,问这m个全排列中有几个公共子串. 思路: 首先单个的数字先计算到答案中,有n个. 然后考虑多个数字,如果有两个数字相邻,那么在m个串中必定都能找到这两个 ...

  3. [题解]Codeforces Round #519 - D. Mysterious Crime

    [题目] D. Mysterious Crime [描述] 有m个n排列,求一共有多少个公共子段. 数据范围:1<=n<=100000,1<=m<=10 [思路] 对于第一个排 ...

  4. 【Codeforces Round #519 by Botan Investments D】Mysterious Crime

    [链接] 我是链接,点我呀:) [题意] 相当于问你这m个数组的任意长度公共子串的个数 [题解] 枚举第1个数组以i为起点的子串. 假设i..j是以i开头的子串能匹配的最长的长度. (这个j可以给2. ...

  5. Codeforces Round #519 D - Mysterious Crime

    题目 题意: 在m组数,每组有n个数(数的范围1-n)中,找到某些序列 使它是每组数的一个公共子序列,问这样的某些序列的个数? 思路: 不难想出答案ans是≥n的. 创立一个next数组,使每组中第i ...

  6. D. Mysterious Crime

    链接 [http://codeforces.com/contest/1043/problem/D] 题意 给你一个m*n的矩阵(m<=10,n<=1e5), 每一行的数字是1到n里不同的数 ...

  7. CF1043D Mysterious Crime

    思路: 参考了http://codeforces.com/blog/entry/62797,把第一个序列重标号成1,2,3,...,n,在剩下的序列中寻找形如x, x + 1, x + 2, ...的 ...

  8. cf1043E. Mysterious Crime(二分 前缀和)

    题意 题目链接 Sol 考场上做完前四题的时候大概还剩半个小时吧,那时候已经困的不行了. 看了看E发现好像很可做?? 又仔细看了几眼发现这不是sb题么... 先考虑两个人,假设贡献分别为\((x, y ...

  9. cf1043D. Mysterious Crime(枚举)

    题意 题目链接 给出\(m\)个长度为\(n\)的排列,问有多少连续公共子串 \(m \leqslant 10, n \leqslant 10^5\) Sol 非常naive的一道题然而交了3遍才过( ...

随机推荐

  1. django 快速实现注册(四)

    一.创建项目与应用  #创建项目fnngj@fnngj-H24X:~/djpy$ django-admin.py startproject mysite3fnngj@fnngj-H24X:~/djpy ...

  2. python并发——从线程池获取返回值

    并发是快速处理大量相似任务的绝佳办法,但对于有返回值的方法,需要一个容器专门来存储每个进程处理完的结果 from multiprocessing import Pool import time #返回 ...

  3. moveUp()

    这个函数内容有点多,想讲一下大概思路: 向上移有两种情况1.前面为空白 这种情况有两个步骤 (1)将人当前的位置设置为空白(0), (2)再讲人前面的位置设置为人(2)2.前面为箱子 当前面为箱子时有 ...

  4. Spring学习随笔(2):Eclipse下Spring环境配置+入门项目

    1 准备工作 (按需下载) Eclipse 下载:http://www.eclipse.org/downloads/eclipse-packages/ : Spring 下载:http://repo. ...

  5. nginx实现负载均衡、缓存功能实战

    nginx实现负载均衡.缓存功能实战 什么是正向代理?应用场景:翻墙 什么是反向代理?例如:haproxy和nginx   Nginx实现反向代理 nginx代理基于是ngx_http_proxy_m ...

  6. Maven-导入本地 Jar 包

    一个 Jar 包 <dependency> <groupId>local</groupId> <artifactId>aliyun-java-sdk-c ...

  7. java常用的正则表达式的工具类

    import com.google.common.base.Strings; import java.util.regex.Matcher;import java.util.regex.Pattern ...

  8. shell脚本:统计分析 /home/ 目录用户磁盘使用情况

    一.统计单台机器 /home/ 目录下磁盘空间使用 top3 的用户 common.sh 脚本用于统计 /home/* 目录下存储空间 top3 的用户. du -sb /home/* |sort - ...

  9. Java数组(3):创建测试数据

    有时我们需要使用数组批量创建测试数据,接下来通过以下4点来举例. (1) 使用Arrays.fill()填充数据 (2) 使用Random类中JDK1.8提供的新方法用来生成随机数 (3) 一个随机数 ...

  10. Java内部类(5):应用例

    例1-闭包(Closure) 闭包是一个可调用的对象(通过Callback),它记录了一些信息,这些信息来自于创建它的作用域 interface Incrementable { void increm ...