http://acm.hdu.edu.cn/showproblem.php?pid=2609

How many

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1707    Accepted Submission(s): 693

Problem Description
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.
 
Input
The input contains multiple test cases.
Each test case include: first one integers n. (2<=n<=10000)
Next n lines follow. Each line has a equal length character string. (string only include '0','1').
 
Output
For each test case output a integer , how many different necklaces.
 
Sample Input
4
0110
1100
1001
0011
4
1010
0101
 
1000
0001
 
Sample Output
1
2
 
 
用到了上一题的查找最小字典串的方法
一看题没思路了,看看学长是用字典树写的,还从没有用字典树写过题,勉强写了,不过这次可以好好体会体会字典树,但既然是在KMP专题里出现的,想必应该也可以用KMP来写吧,再搜搜

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm> using namespace std; const int maxn = ; char s1[maxn], s[maxn], a[maxn]; struct node
{
struct node *next[];
}; int GetMin(char S[], int N)
{
int i=, j=; while(i<N && j<N)
{
int k = ; while(S[i+k]==S[j+k] && k<N)
k++; if(k==N)
break; if(S[i+k]<S[j+k])
{
if(j+k>i)
j += k+;
else
j = i+;
}
else
{
if(i+k>j)
i += k+;
else
i = j+;
}
} return min(i, j);
} int FindTree(node *head, char s[])
{
node *p=head;
int i, flag = ; for(i=; s[i]; i++)
{
int k = s[i]-''; if(p->next[k]==NULL)
{
flag = ;
p->next[k] = new node();
} p = p->next[k];
} return flag;
} void Free(node *head)
{
node *p = head;
for(int i=; i<; i++)
if(p->next[i]!=NULL)
Free(p->next[i]);
free(p);
} int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int sum = ;
node *head = new node(); while(n--)
{
scanf("%s", s1);
int len = strlen(s1); strcpy(a, s1);
strcat(a, s1); int index = GetMin(a, len);
strncpy(s, a+index, len); int ans = FindTree(head, s); if(ans) sum ++;
} printf("%d\n", sum); Free(head);
}
return ;
}

(字典树)How many--hdu--2609的更多相关文章

  1. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  2. HDU 5687 字典树插入查找删除

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...

  3. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  4. HDU 4287 Intelligent IME(字典树数组版)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  6. HDU 4757 Tree 可持久化字典树

    Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...

  7. HDU 5536 Chip Factory 字典树

    Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  8. hdu 4099 Revenge of Fibonacci 字典树+大数

    将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...

  9. HDU 1247 - Hat’s Words - [字典树水题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...

  10. HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...

随机推荐

  1. 根据条件决定是否为input设置只读属性

    代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...

  2. pycharm 远程开发

    1. 服务器安装图形化 和 pycharm 本地使用 MobaXterm 工具登陆 session配置 勾选 x11-forwarding 运行pycharm.sh 2. 本地pycharm 远程服务 ...

  3. New Document (2)

    #Markdown 语法说明 (简体中文版) / (点击查看快速入门) ##概述 ###宗旨 兼容 HTML 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 ...

  4. SpringMVC源码总结(一)HandlerMapping和HandlerAdapter入门

    SpringMVC在使用过程中,大多是使用注解,对它的实现接口之类的关系理解变得模糊, 通过对XML配置的理解,可以理清各个类的关系,譬如控制器类要实现Controller接口. 接触SpringMV ...

  5. 微信小程序及开发工具介绍

    http://mp.weixin.qq.com/wiki  这里下载开发者工具

  6. Pocket Cube

    Pocket Cube http://acm.hdu.edu.cn/showproblem.php?pid=5983 Time Limit: 2000/1000 MS (Java/Others)    ...

  7. New Game! (最短路+建图)

    New Game! https://www.nowcoder.com/acm/contest/201/L 题目描述 Eagle Jump公司正在开发一款新的游戏.Hifumi Takimoto作为其中 ...

  8. 【校招面试 之 C/C++】第30题 C++ 11新特性(一)之auto关键字

    1.自动类型推断 auto自动类型推断,用于从初始化表达式中推断出变量的数据类型.通过auto的自动类型推断,可以大大简化我们的编程工作.下面是一些使用auto的例子. #include <ve ...

  9. java web 常用正则

    什么是 RegExp? RegExp 是正则表达式(Regular expression)的缩写,作用是对字符串执行模式匹配. 通常用于格式验证.正则替换.查找子串等 各种编程语言的正则表达式基本相同 ...

  10. 简单的几个Boost定时器

    boost的asio库里有几个定时的器,老的有 deadline_timer , 还有三个可配合 C++11 的 chrono 使用的 high_resolution_timer  . steady_ ...