似乎是归队赛的最后一道题。

由于当时以为是公共字串所以没写555555,其实是求公共前缀。

做法是建立tire,把tire上的点编号看成是值,查询第l到第r个字符串的区间内不重复的值的个数。建立主席树维护即可

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define dow(i,l,r) for(int i=r;i>=l;i--)
#define LL long long
#define maxn 100100
#define mm 100000
#define maxm 8000000
using namespace std; int lson[maxm],rson[maxm],size[maxm],root[maxn],num[maxn],n,m,toti,totr,tot,son[maxn][],pre[maxn];
char s[maxn]; int more1()
{
++toti;
memset(son[toti],,sizeof(son[toti]));
return toti;
} int more2()
{
++totr;
lson[totr]=;
rson[totr]=;
size[totr]=;
return totr;
} void add(int &x,int old,int l,int r,int y,int z)
{
x=more2();
lson[x]=lson[old];
rson[x]=rson[old];
size[x]=size[old]+z;
if (l==r) return;
int mid=(l+r)>>;
if (y<=mid) add(lson[x],lson[old],l,mid,y,z);
else add(rson[x],rson[old],mid+,r,y,z);
} int ask(int x,int y,int l,int r)
{
// printf("%d %d %d %d %d %d %d\n",x,y,l,r,size[x],size[lson[x]],size[rson[x]]);
if (!x) return ;
if (l==r) return size[x];
int mid=(l+r)>>;
if (y<=mid) return ask(lson[x],y,l,mid)+size[rson[x]];
return ask(rson[x],y,mid+,r);
} int main()
{
while (scanf("%d",&n)!=EOF) {
memset(pre,,sizeof(pre));
toti=;
more1();
totr=;
tot=;
rep(i,,n) {
scanf("%s",s);
int len=strlen(s);
int u=;
root[i]=root[i-];
rep(j,,len-) {
// printf("\t%d\n",u);
++tot;
if (!j) num[i]=tot;
int k=s[j]-'a';
if (!son[u][k]) son[u][k]=more1();
// printf("\t%d\n",u);
add(root[i],root[i],,mm,tot,);
if (pre[son[u][k]]) {
// printf("\t\t%d\n",pre[son[u][k]]);
add(root[i],root[i],,mm,pre[son[u][k]],-);
}
pre[son[u][k]]=tot;
u=son[u][k];
// printf("\t%d\n",u);
}
// printf("%d\n",size[root[i]]);
}
scanf("%d",&m);
int z=;
while (m--) {
int j,k;
scanf("%d %d",&j,&k);
int l=min((j+z)%n+,(k+z)%n+),r=max((j+z)%n+,(k+z)%n+);
// printf("%d %d\n",l,r);
printf("%d\n",z=ask(root[r],num[l],,mm));
}
}
return ;
}

【HUD-5790】Prefix (主席树+tire)的更多相关文章

  1. HDU 5790 Prefix(Hash + 主席树)

    题目链接  Prefix 题意  给定一个字符串序列,求第$l$个字符串到第$r$个字符串之间有多少个不同的前缀 强制在线 考虑$Hash$ 首先把所有前缀都$hash$出来,按顺序组成一个长度不超过 ...

  2. 【HDU - 5790 】Prefix(主席树+Trie树)

    BUPT2017 wintertraining(15) #7C 题意 求[min((Z+L)%N,(Z+R)%N)+1,max((Z+L)%N,(Z+R)%N)+1]中不同前缀的个数,Z是上次询问的结 ...

  3. hdu5790 Prefix(Trie树+主席树)

    Problem Description Alice gets N strings. Now she has Q questions to ask you. For each question, she ...

  4. HDU5790 Prefix 字典树+主席树

    分析:这个题和spoj的d_query是一个题,那个是求一段区间里有多少个不同的数字,这里是统计有多少个不同的前缀 用字典树进行判重,(和查询不同的数字一样)对于每个不同的前缀,只保留它最后一次出现的 ...

  5. SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

  6. ZOJ - 2112 Dynamic Rankings(BIT套主席树)

    纠结了好久的一道题,以前是用线段树套平衡树二分做的,感觉时间复杂度和分块差不多了... 终于用BIT套函数式线段树了过了,120ms就是快,此题主要是卡内存. 假设离散后有ns个不同的值,递归层数是l ...

  7. SPOJ 10628 Count on a tree(Tarjan离线 | RMQ-ST在线求LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

  8. bzoj3207--Hash+主席树

    题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...

  9. bzoj1901--树状数组套主席树

    树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...

随机推荐

  1. VINS(七)estimator_node 数据对齐 imu预积分 vision

    首先通过vins_estimator mode监听几个Topic(频率2000Hz),将imu数据,feature数据,raw_image数据(用于回环检测)通过各自的回调函数封装起来 ros::Su ...

  2. hdu1217Arbitrage(floyd+map)

    Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  3. Qt-QML-Repeater-导航条

    上篇文章中,我写了一个自己的Button,也就是美化了一下QML自带的Button 就是上面的这个,剩下的就是放三张图片在上面就可以了,当然了,这个Button在后期,还是会加入更让多的美化,比如,可 ...

  4. Hexo 博客 之 腾讯云部署过程

    写在前面 Hexo 博客搭好了有差不多两周时间了,这期间走了很多弯路,跳了很多坑.一些坑自己 bing 到了答案,找到了解决方法,一些坑则是自己摸索出来的解决方法.现在准备写几篇关于搭建流程.搭建过程 ...

  5. 最小生成树(II)与Kruskal算法

    为防止网页加载过慢,故分两章.上接https://www.cnblogs.com/Uninstalllingyi/p/10479470.html Kruskal算法——将森林合并成树 玩过瘟疫公司吗… ...

  6. LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )

    1.题目:原题链接 Given an integer, write a function to determine if it is a power of two. 给定一个整数,判断该整数是否是2的 ...

  7. UESTC 1717 Journey(DFS+LCA)(Sichuan State Programming Contest 2012)

    Description Bob has traveled to byteland, he find the N cities in byteland formed a tree structure, ...

  8. 2d命令行小游戏源码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. 内存转储文件调试系统崩溃bug

    百度百科:内存转储文件 内存转储是用于系统崩溃时,将内存中的数据转储保存在转储文件中,供给有关人员进行排错分析用途.而它所保存生成的文件就叫做内存转储文件. 内存转储文件也被称作虚拟内存,它是用硬盘里 ...

  10. 项目--uml

    [团队信息] 团队项目: 小葵日记--主打记录与分享模式的日记app 队名:日不落战队 队员信息及贡献分比例: 短学号 名 本次作业博客链接 此次作业任务 贡献分配 备注 501 安琪 http:// ...