BZOJ 3555: [Ctsc2014]企鹅QQ
似乎大家全部都用的是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的更多相关文章
- BZOJ 3555: [Ctsc2014]企鹅QQ [字符串哈希]【学习笔记】
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2046 Solved: 749[Submit][Statu ...
- BZOJ 3555: [Ctsc2014]企鹅QQ hash
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- bzoj——3555: [Ctsc2014]企鹅QQ
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2617 Solved: 921[Submit][Statu ...
- 字符串Hash || BZOJ 3555: [Ctsc2014]企鹅QQ || P4503 [CTSC2014]企鹅QQ
题面:[CTSC2014]企鹅QQ 题解:无 代码: #include<iostream> #include<cstring> #include<cstdio> # ...
- bzoj 3555: [Ctsc2014]企鹅QQ【hash+瞎搞】
首先注意 先hash一下,双hash,然后枚举删去位置,把hash值排个序,把些相等的加起来统计一下对数即可 #include<iostream> #include<cstdio&g ...
- 3555: [Ctsc2014]企鹅QQ
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 696 Solved: 294[Submit][Status ...
- bzoj3555: [Ctsc2014]企鹅QQ
将字符串hash.不难写.然而1.注意用longlong2.数组大小注意...3.似乎别人都用的unsigned long long ?. #include<cstdio> #includ ...
- [Ctsc2014]企鹅QQ
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description P ...
- 【BZOJ3555】 [Ctsc2014]企鹅QQ
BZOJ3555 [Ctsc2014]企鹅QQ Solution 只需要前缀Hash,然后考虑每一段的贡献就好了!!! 代码实现 #include<stdio.h> #include< ...
随机推荐
- zookeeper 版本不一致导致不断重连
在使用kafka 和zookeeper 实现实时分析程序时,由于zookeeper部署版本和分析程序导入jar包的版本不一致,导致了当实时分析程序从远程服务器连接kafka集群的zookeeper时报 ...
- Java GUI程序设计
在实际应用中,我们见到的许多应用界面都属于GUI图形型用户界面.如:我们点击QQ图标,就会弹出一个QQ登陆界面的对话框.这个QQ图标就可以被称作图形化的用户界面. 其实,用户界面的类型分为两类:Com ...
- 【Loadrunner】Loadrunner Vuser 两种运行方式【error:not enough memory解决方案】
Loadrunner Vuser 两种运行方式 报错如下解决方案: 报错原因:都消息内存,之前用户是按线程跑,一个进程开了多个线程,其中有部分内存是这些线程共享的,出错应该是内存出现冲突了不够用了.现 ...
- mysql 数据操作 单表查询 目录
mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...
- 【剑指offer】矩形覆盖
一.题目: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 二.思路: 斐波那契数列 三.代码:
- 阻止提交按钮的默认 action
使用 preventDefault() 函数来阻止对表单的提交. 示例代码如下: <html><head><script type="text/javascri ...
- 置换检验(Permutation Test)学习[转载]
转自:https://www.cnblogs.com/bnuvincent/p/6813785.html http://www.bioinfo-scrounger.com/archives/564 1 ...
- yum && 编译 安装mysql 5.7 多实例
yum安装 [root@localhost ~]# wget http://repo.mysql.com/mysql57-community-release-el7.rpm [root@localho ...
- selenium python 启动Firefox
我的火狐浏览器版本是最新的: 下载geckodrive:https://github.com/mozilla/geckodriver/releases/ 下载完后将exe文件放到这里“D:\firef ...
- X-Forwarded-For 负载均衡 7 层 HTTP 模式获取来访客户端真实 IP 的方法(IIS/Apache/Nginx/Tomcat)
https://help.aliyun.com/knowledge_detail/13051859.html?pos=1 1.IIS 6 配置方案2.IIS 7 配置方案3.Apache 配置方案4. ...