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. Linux逻辑卷的拉伸和缩小

    相对于普通磁盘优势是逻辑卷可以在线动态的拉伸(不用先unmount操作),只要vg中有足够的空间即可 1:首先查看vg是否有足够的空间 [root@gechong mapper]# vgdisplay ...

  2. 1Java语言概述——重拾Java

    1.1 Java 语言诞生 Java 是1995 年 由 Sun公司开发的革命性编程语言. 1.2 Java 的特点 序号 主题 描述 1 简单 比C++简单 2 面向对象 基于对象的编程更符合人的思 ...

  3. git基本命令之删除撤销操作

    1.将删除文件恢复--撤销所删除的文件git checkout 文件名 2.git resetgit reset --hard commitID(或某个节点)----强制切换到某个点,会导致所修改的内 ...

  4. oozie错误:javax.servlet.jsp.el.ELException: variable [***] cannot be resolved

    完整错误: javax.servlet.jsp.el.ELException: variable [compute] cannot be resolved at org.apache.oozie.ut ...

  5. vortex

    vortex - Bing dictionary US['vɔr.teks]UK['vɔː(r)teks] n.旋涡:涡旋:低涡:感情(或局势)的旋涡 网络漩涡:涡流:旋风 变形Plural Form ...

  6. 解决SpringBoot中webScocket不能注入bean的问题

    最近在做websocket聊天,但是遇到一个问题,就是在websocket的服务中要调用Service层的东西.首先我想到的是通过@Autowire注解来实现bean的注入.但是进过测试发现,注入的b ...

  7. ios runtime简单实用(添加动态属性)

    #import "Person.h" @interface Person (PersonCategory)   // 添加Person中没有的name属性 @property (n ...

  8. TextView 小技巧

    1.TextView的单行显示 最后空间不够的画变为省略号显示 textView中让结尾变为省略号android:ellipsize = "end" 省略号在结尾tv.setEll ...

  9. js 递归调用

    js递归调用 function fact(num) { ) { ; } else { ); } } 以下代码可导致出错: var anotherFact = fact; fact = null; al ...

  10. JFinal开发框架简介

    JFinal 中的Controller Controller是JFinal核心类之一,该类作为MVC模式中的控制器.基于JFinal的Web应用的控制器需要继承该类.Controller是定义Acti ...