hdu2609 How many 字典树+最小表示法
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me
How many kinds of necklaces total have.(if two necklaces can equal by rotating ,we say the two necklaces are some).
For example 0110 express a necklace, you can rotate it. 0110 -> 1100 -> 1001 -> 0011->0110.
题意:给出若干字符串,问在循环同构算作同一种字符串的条件下,有多少不同字符串
对所有串进行最小表示法的处理,去除同构情况,再将最小表示后的串插入字典树,并统计种类数。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn=1e5+;
const int maxm=8e5+;
char s[maxn<<]; inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;} int nxt[maxm][]; //字母结点
int tail[maxm]; //记录某个结点是否为单词结尾,可以用bool型仅记录是否结尾,也可以int型记录其作为结尾的单词编号或记录单词出现过多少次
int size,cnt; void init(){ //初始化函数
nxt[][]=nxt[][]=;
memset(tail,,sizeof(tail));
size=;
cnt=;
} void insert(char s[]){ //添加单词函数
int p=;
for(int i=;s[i];i++){
int &x=nxt[p][s[i]-''];
if(!x){
nxt[size][]=nxt[size][]=;
x=size++;
}
p=x;
}
if(!tail[p]){
cnt++;
tail[p]=;
}
} int MINR(char s[],int l){
for(int i=;i<l;++i)s[l+i]=s[i];
s[*l]=;
int i=,j=;
while(i<l&&j<l){
int k=;
while(s[i+k]==s[j+k]&&k<l)++k;
if(k==l)return min(i,j);
if(s[i+k]>s[j+k])i=max(i+k+,j+);
else j=max(j+k+,i+);
}
return min(i,j);
} int main(){
int n;
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;++i){
scanf("%s",s);
int l=strlen(s);
int pos=MINR(s,l);
s[pos+l]=;
insert(s+pos);
}
printf("%d\n",cnt);
}
return ;
}
hdu2609 How many 字典树+最小表示法的更多相关文章
- 双01字典树最小XOR(three arrays)--2019 Multi-University Training Contest 5(hdu杭电多校第5场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625 题意: 给你两串数 a串,b串,让你一一配对XOR使得新的 C 串字典序最小. 思路: 首先这边 ...
- POJ 1635 树的最小表示法/HASH
题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...
- [BZOJ4337][BJOI2015]树的同构(树的最小表示法)
4337: BJOI2015 树的同构 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1023 Solved: 436[Submit][Status ...
- HDU2609 How many —— 最小表示法
题目链接:https://vjudge.net/problem/HDU-2609 How many Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- 『Tree nesting 树形状压dp 最小表示法』
Tree nesting (CF762F) Description 有两个树 S.T,问 S 中有多少个互不相同的连通子图与 T 同构.由于答案 可能会很大,请输出答案模 1000000007 后的值 ...
- POJ1635 Subway tree systems ——(判断树的同构,树的最小表示法)
给两棵有根树,判断是否同构.因为同构的树的最小表示法唯一,那么用最小表示法表示这两棵树,即可判断同构.顺便如果是无根树的话可以通过选出重心以后套用之前的方法. AC代码如下: #include < ...
- luogu P5043 【模板】树同构 hash 最小表示法
LINK:模板 树同构 题目说的很迷 给了一棵有根树 但是重新标号 言外之意还是一棵无根树 然后要求判断是否重构. 由于时无根的 所以一个比较显然的想法暴力枚举根. 然后做树hash或者树的最小表示法 ...
- hdu2609 How many【最小表示法】【Hash】
How many Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu2609 最小表示法
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
随机推荐
- centos7.0 64位系统 安装PHP5.3 支持 nginx
1 安装PHP所需要的扩展 yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel curl cur ...
- learning scala 数组和容器
数组:可变的,可索引的,元素具有相同类型的数据集合 一维数组 scala> val intValueArr = new Array[Int](3)intValueArr: Array[Int] ...
- python-第一类对象,闭包,迭代器
# def fn(): # print("我叫fn") # fn() # print(fn) # <function fn at 0x0000000001D12E18> ...
- Linux下使用bind,epoll对网络编程封装
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- vue引用样式
cnpm i sass-loader node-sass -D <link rel="stylesheet" href="./static/reset.css&qu ...
- Docker小白从零入门实战
环境:Centos 6.9 0.查看是否满足安装需求. 先检查服务器环境,docker要求操作系统CentOS6以上,kernel 版本必须2.6.32-431或更高,即>=CentOS 6.5 ...
- loadrunner json参数化
因为json格式有{},所以LR参数化时如果也用默认的{}的话,会冲突,这样脚本运行时就无法识别,导致不能正确的读取参数化文件里的内容,此时把参数化的{}改成其他符号即可,比如<>
- EF6增改删等常用基类
using System; using System.Linq; using System.Threading.Tasks; using System.Linq.Expressions; using ...
- L323 英语有必要学语法吗
The Agony and Ecstasy of Grammar “Underline a relative clause.” This challenge would give a lot of a ...
- springsecurity基于数据库验证用户
之前的springsecurity程序都是将数据存放在内存中的,通过 <security:user-service> <security:user name="user&q ...