(字典树)How many--hdu--2609
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
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.
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').
代码:
#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的更多相关文章
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- HDU 4287 Intelligent IME(字典树数组版)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- HDU 4757 Tree 可持久化字典树
Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- hdu 4099 Revenge of Fibonacci 字典树+大数
将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...
随机推荐
- 根据条件决定是否为input设置只读属性
代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- pycharm 远程开发
1. 服务器安装图形化 和 pycharm 本地使用 MobaXterm 工具登陆 session配置 勾选 x11-forwarding 运行pycharm.sh 2. 本地pycharm 远程服务 ...
- New Document (2)
#Markdown 语法说明 (简体中文版) / (点击查看快速入门) ##概述 ###宗旨 兼容 HTML 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 ...
- SpringMVC源码总结(一)HandlerMapping和HandlerAdapter入门
SpringMVC在使用过程中,大多是使用注解,对它的实现接口之类的关系理解变得模糊, 通过对XML配置的理解,可以理清各个类的关系,譬如控制器类要实现Controller接口. 接触SpringMV ...
- 微信小程序及开发工具介绍
http://mp.weixin.qq.com/wiki 这里下载开发者工具
- Pocket Cube
Pocket Cube http://acm.hdu.edu.cn/showproblem.php?pid=5983 Time Limit: 2000/1000 MS (Java/Others) ...
- New Game! (最短路+建图)
New Game! https://www.nowcoder.com/acm/contest/201/L 题目描述 Eagle Jump公司正在开发一款新的游戏.Hifumi Takimoto作为其中 ...
- 【校招面试 之 C/C++】第30题 C++ 11新特性(一)之auto关键字
1.自动类型推断 auto自动类型推断,用于从初始化表达式中推断出变量的数据类型.通过auto的自动类型推断,可以大大简化我们的编程工作.下面是一些使用auto的例子. #include <ve ...
- java web 常用正则
什么是 RegExp? RegExp 是正则表达式(Regular expression)的缩写,作用是对字符串执行模式匹配. 通常用于格式验证.正则替换.查找子串等 各种编程语言的正则表达式基本相同 ...
- 简单的几个Boost定时器
boost的asio库里有几个定时的器,老的有 deadline_timer , 还有三个可配合 C++11 的 chrono 使用的 high_resolution_timer . steady_ ...