似乎大家全部都用的是hash?那我讲一个不用hash的做法吧。

首先考虑只有一位不同的是哪一位,那么这一位前面的位上的字符一定是全部相同,后面的字符也是全部相同。首先考虑后面的字符。

我们对n个串的反串建trie树,这样,每一个后缀就对应一个trie树上的唯一一个节点,不同的后缀对应的就是不同的节点,这样就不用hash表了。

但是字符集很大,用trie空间太大,那么就用邻接表或map存边就好了。

然后就是令前缀全部相同。那么我们从左到右枚举每一位,按照当前位的字符进行分治,当前位不同的就通过后缀计算贡献,相同的就放在一起,继续分治即可。

然后就跑的飞快,目前是BZOJ rk2,Luogu rk1。

实现的时候有很多细节,具体请参考代码。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define vi vector<int>
#define vit vector<int>::iterator
#define pir pair<int,int>
#define fr first
#define sc second
#define mp(x,y) make_pair(x,y)
#define rsort(x,y) sort(x,y),reverse(x,y)
using namespace std; inline char gc() {
// static char buf[100000],*p1,*p2;
// return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
return getchar();
} template<class T>
int read(T &ans) {
ans=0;char ch=gc();T f=1;
while(!isdigit(ch)) {
if(ch==EOF) return -1;
if(ch=='-') f=-1;
ch=gc();
}
while(isdigit(ch))
ans=ans*10+ch-'0',ch=gc();
ans*=f;return 1;
} template<class T1,class T2>
int read(T1 &a,T2 &b) {
return read(a)!=EOF&&read(b)!=EOF?2:EOF;
} template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
} typedef long long ll;
const int Maxn=6100000;
const int Maxm=31000;
const int Maxl=210;
const int inf=0x3f3f3f3f; int to[Maxn],nxt[Maxn],first[Maxn],tot=1;
char w[Maxn],s[Maxm][Maxl];
int p[Maxm][Maxl],n,len,x,po[Maxl][Maxl],qq[Maxl],bj[Maxl],ans;
int a[Maxm],siz[Maxl],b[Maxm],tn[Maxn],cnt=1;
queue<int> q[Maxl]; inline void add(int u,int v,char wi) {
to[tot]=v;
nxt[tot]=first[u];
w[tot]=wi;
first[u]=tot++;
} void solve(int l,int r,int cur) {
if(l>r) return ;
if(cur==len) return ;
int cnt=0;
for(int i=l;i<=r;i++) {
int x=s[a[i]][cur];
q[x].push(a[i]);
siz[x]++;
if(!bj[x]) qq[++cnt]=x,bj[x]=1;
}
if(cnt==1) {
for(int i=1;i<=cnt;i++) bj[qq[i]]=siz[qq[i]]=0;
while(!q[qq[1]].empty()) q[qq[1]].pop();
solve(l,r,cur+1);
}
else {
int sxz,zhy=0;
for(int i=1;i<=cnt;i++) {
if(siz[qq[i]]>zhy) {
sxz=i;
zhy=siz[qq[i]];
}
}
swap(qq[cnt],qq[sxz]);
int las=l;po[cur][0]=las;
for(int i=1;i<cnt;i++) {
int x=qq[i],num=0;
while(!q[x].empty()) {
int now=q[x].front();
q[x].pop();
b[++num]=p[now][cur+1];
ans+=tn[b[num]];
a[las++]=now;
}
while(num) tn[b[num--]]++;
po[cur][i]=las;
}
while(!q[qq[cnt]].empty()) {
int now=q[qq[cnt]].front();
q[qq[cnt]].pop();
ans+=tn[p[now][cur+1]];
a[las++]=now;
} po[cur][cnt]=las;
for(int i=l;i<po[cur][cnt-1];i++) tn[p[a[i]][cur+1]]--;
for(int i=1;i<=cnt;i++) bj[qq[i]]=siz[qq[i]]=0;
for(int i=1;i<=cnt;i++)
solve(po[cur][i-1],po[cur][i]-1,cur+1);
}
} signed main() {
// freopen("test.in","r",stdin);
read(n,len,x);
for(int i=1;i<=n;i++) {
scanf("%s",s[i]);
int now=1;
for(int j=len-1;j>=0;j--) {
int temp=0;
for(int k=first[now];k;k=nxt[k])
if(w[k]==s[i][j]) {
temp=to[k];
break;
}
if(!temp) add(now,++cnt,s[i][j]),temp=cnt;
now=temp;
p[i][j]=now;
}
}
for(int i=1;i<=n;i++) a[i]=i;
solve(1,n,0);
printf("%d\n",ans);
return 0;
}

BZOJ 3555: [Ctsc2014]企鹅QQ的更多相关文章

  1. BZOJ 3555: [Ctsc2014]企鹅QQ [字符串哈希]【学习笔记】

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2046  Solved: 749[Submit][Statu ...

  2. BZOJ 3555: [Ctsc2014]企鹅QQ hash

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  3. bzoj——3555: [Ctsc2014]企鹅QQ

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2617  Solved: 921[Submit][Statu ...

  4. 字符串Hash || BZOJ 3555: [Ctsc2014]企鹅QQ || P4503 [CTSC2014]企鹅QQ

    题面:[CTSC2014]企鹅QQ 题解:无 代码: #include<iostream> #include<cstring> #include<cstdio> # ...

  5. bzoj 3555: [Ctsc2014]企鹅QQ【hash+瞎搞】

    首先注意 先hash一下,双hash,然后枚举删去位置,把hash值排个序,把些相等的加起来统计一下对数即可 #include<iostream> #include<cstdio&g ...

  6. 3555: [Ctsc2014]企鹅QQ

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 696  Solved: 294[Submit][Status ...

  7. bzoj3555: [Ctsc2014]企鹅QQ

    将字符串hash.不难写.然而1.注意用longlong2.数组大小注意...3.似乎别人都用的unsigned long long ?. #include<cstdio> #includ ...

  8. [Ctsc2014]企鹅QQ

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description P ...

  9. 【BZOJ3555】 [Ctsc2014]企鹅QQ

    BZOJ3555 [Ctsc2014]企鹅QQ Solution 只需要前缀Hash,然后考虑每一段的贡献就好了!!! 代码实现 #include<stdio.h> #include< ...

随机推荐

  1. SVN版本控制系统搭建(结合http服务)

    SVN版本控制服务器搭建 Svn(subversion)是一个开源代码管理的控制系统,用来管理和存储开发的源代码,基于C/S模式.可以单独提供服务,也可以结合http服务来实现. 运行方式  运行端口 ...

  2. (四)Web应用开发---新增对象Master类别

    UI设计类似如下形式 Copy如下Html代码到新创建的Html <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xh ...

  3. sparkuser is not in the sudoers file. This incident will be reported.

    切换到root身份$su -(注意有- ,这和su是不同的,在用命令"su"的时候只是切换到root,但没有把root的环境变量传过去,还是当前用户的环境变量,用"su ...

  4. IO流(6)获取功能

    获取功能: * public String getAbsolutePath():获取绝对路径 * public String getPath():获取相对路径 * public String getN ...

  5. 理解CopyOnWriteArrayList

    CopyOnWriteArrayList,顾名思义,Write的时候总是要Copy,也就是说对于任何可变的操作(add.set.remove)都是伴随复制这个动作的 A thread-safe var ...

  6. 冒泡排序快速版(C)

    冒泡排序C语言版:在每轮排序中检查时候有元素位置交换,如果无交换,说明数组元素已经有序,无需继续排序 #include <stdio.h> #include <stdlib.h> ...

  7. mysql python pymysql模块 增删改查 查询 fetchone

    import pymysql mysql_host = '192.168.0.106' port = 3306 mysql_user = 'root' mysql_pwd = ' encoding = ...

  8. Dom最常用的API

    document方法: getElementById(id) Node 返回指定结点的引用 getElementsByTagName(name) NodeList 返回文档中所有匹配的元素的集合 cr ...

  9. [vue]vue-book

    我们打算要做这几个模块 首页 列表 收藏 添加 home.vue --> list.vue -->app.vue --> main.js 安装环境 npm i vue-cli -g ...

  10. 主成分分析 PCA算法原理

    对同一个体进行多项观察时,必定涉及多个随机变量X1,X2,…,Xp,它们都是的相关性, 一时难以综合.这时就需要借助主成分分析 (principal component analysis)来概括诸多信 ...