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. CTR点击率预估干货分享

    CTR点击率预估干货分享 http://blog.csdn.net/bitcarmanlee/article/details/52138713

  2. Anaconda中python加入环境变量

    1.我的电脑---高级系统设置 2.选中环境变量,保存. 3.在系统环境变量PATH中,加入Anaconda3及Script路径加入其中 4.测试python

  3. luoguP1004 方格取数(四维DP)

    题目链接:https://www.luogu.org/problemnew/show/P1004 思路: 这道题是四维DP的模板题,与luoguP1006传纸条基本相似,用f[i][j][k][l]表 ...

  4. Django的mode的分组查询和聚合查询和F查询和Q查询

    1.聚合查询 # 聚合函数aggregate,求指定字段的最大值,最小值,平均值,和的值,方法如下 from django.db.models import Avg from django.db.mo ...

  5. [leetcode]689. Maximum Sum of 3 Non-Overlapping Subarrays三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  6. SVN:项目管理工具

    svn:项目管理工具. 我们在进行团队开发的时候,每个人负责不同的层,比如:A负责DAO层,B负责SERVICE层,C负责DOMAIN层.我们开发完了自己管理的各层后需要将各层整合在一起,肯定不是拿U ...

  7. 【原创】VB超强游戏外挂帮助类,封装了很多方法

    ''' <summary> ''' a very nice file that can be used on other projects ''' </summary> ''' ...

  8. 全屏API

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2679 二.相关文章以 ...

  9. JS 读取本地Excel文件

    首先我们先引用一个Excel的类库xlsx.full.min.js 中间处理: 'use strict'; var ExcelReader = { isFirstRead: true, fixdata ...

  10. 参看dll参数类型

    http://blog.csdn.net/chinabinlang/article/details/7698459 验证