How many---hdu2609(最小表示)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2609
给你n个01串,然后求这n个串中有几个不同的;
例如:1100 ,1001 ,0011 ,0110,这4个串都是相同的,因为他们可以通过移动得到;
我们可以通过最大最小表示法来求出串的最小字典序所对应的串,那么上面的四个串对应的都是0011,这样一来就可以很轻松的求出结果了;
后面的我们可以用把每次求得串插入到Trie树中,如果树中已经存在此串,那么就返回0,不存在就返回1;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std; const int N = 2e4+;
char s[N], s0[N]; struct node
{
node* next[];
}; int Min_Index(char a[], int n)
{
int i=, j=, k=;
while(i<n && j<n && k<n)
{
int t = a[(i+k)%n] - a[(j+k)%n];
if(t == )
k++;
else
{
if(t < )
j = j + k + ;
else
i = i + k + ;
if(i == j)
j++;
k=;
}
}
return min(i, j);
}
int Tire(char a[], node* head)
{
int flag = ;
node *p = head;
for(int i=; a[i]; i++)
{
int k = a[i]-'';
if(p->next[k]==NULL)
{
flag = ;///建立字典树,如果a已经存在返回0,否则返回1,加到结果中去;
p->next[k] = new node();
}
p = p->next[k];
}
return flag;
}
void FreeTire(node *head)///没有用到,但是还是留着当个知识点吧,释放内存;
{
node *p = head; for(int i=; i<; i++)
{
if(p->next[i] != NULL)
FreeTire(p->next[i]);
}
free(p);
} int main()
{
int n, ans, len, Min;
node*head;
while(scanf("%d", &n)!=EOF)
{
head = new node();
ans = ;
for(int i=; i<=n; i++)
{
scanf("%s", s0);
len = strlen(s0); strcpy(s, s0);
strcat(s, s0); Min = Min_Index(s, len);
memset(s0, , sizeof(s0));
strncpy(s0, s+Min, len); ans+=Tire(s0, head);
}
printf("%d\n", ans); /// FreeTire(head); }
return ;
}
不用字典树:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std; const int N = ;
char s[N], s0[N], ss[N*N][N]; int Min_Index(char a[], int n)
{
int i=, j=, k=;
while(i<n && j<n && k<n)
{
int t = a[(i+k)%n] - a[(j+k)%n];
if(t == )
k++;
else
{
if(t < )
j = j + k + ;
else
i = i + k + ;
if(i == j)
j++;
k=;
}
}
return min(i, j);
}
int cmp(const void *p1, const void *p2)
{
return strcmp((char *)p1, (char *)p2);
}
int main()
{
int n, ans, len, Min, k;
while(scanf("%d", &n)!=EOF)
{
if(n==)
{
printf("0\n");
continue;
}
k = ;
for(int i=; i<=n; i++)
{
scanf("%s", s0);
len = strlen(s0); strcpy(s, s0);
strcat(s, s0); Min = Min_Index(s, len);
memset(s0, , sizeof(s0));
strncpy(s0, s+Min, len);
s0[len] = '\0';
strcpy(ss[k], s0);
k++;
}
qsort(ss, k, sizeof(ss[]), cmp);
ans = ;
for(int i=; i<k; i++)
{
if(strcmp(ss[i], ss[i-])!=)
ans++;
}
printf("%d\n", ans);
}
return ;
}
How many---hdu2609(最小表示)的更多相关文章
- hdu2609 最小表示法
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- hdu2609最小表示法
#include <iostream> #include <algorithm> #include <string.h> #include <cstdio&g ...
- HDU2609 How many —— 最小表示法
题目链接:https://vjudge.net/problem/HDU-2609 How many Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- hdu2609 How many【最小表示法】【Hash】
How many Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu2609 How many 字典树+最小表示法
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell meHow many ...
- hdu2609(最小表示法)
题意:有n个环形字符串,一个环形字符串移动会形成不能的字符串,我们把它们看作同一串字符串,求有多少个不同的字符串....... 思路:用最小表示发将一个环形串的最小字典序找出来,然后让这个环形串按照这 ...
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- BZOJ 1391: [Ceoi2008]order [最小割]
1391: [Ceoi2008]order Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1509 Solved: 460[Submit][Statu ...
- 《徐徐道来话Java》:PriorityQueue和最小堆
在讲解PriorityQueue之前,需要先熟悉一个有序数据结构:最小堆. 最小堆是一种经过排序的完全二叉树,其中任一非终端节点数值均不大于其左孩子和右孩子节点的值. 可以得出结论,如果一棵二叉树满足 ...
- C++ 最小化到托盘
#define WM_SHOWTASK (WM_USER + 1) void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID &a ...
随机推荐
- Makefile 11——支持头文件目录指定
现在,是时候在对应目录放入对应文件了: /× foo.h */ #ifndef __FOO_H #define __FOO_H void foo(void) #endif/*__FOO_H*/ /* ...
- Bootstrap学习笔记(8)--响应式导航栏
说明: 1. 响应式导航栏,就是右上角的三道杠,点一下下方出现隐藏的导航栏.如果屏幕够大就显示所有的导航选项,如果屏幕小比如手机,就显示部分,剩下的放到三道杠里隐藏. 2. 外面套一个大的div,其实 ...
- git学习(二):git config命令
不同的git config操作不同的参数文件 git config --global // 配置用户目录下的.gitconfig那文件 git config --system // 配置系统级配置文件 ...
- 使用tcpdump观察IPV4头部结构
sudo tcpdump -nt -i lo #抓取本地回路上的数据包 先运行上面的命令,然后再另一个终端运行下图所示的命令: [root@linux 5]# sudo tcpdump -nt -i ...
- 精心收集的Hadoop学习资料(持续更新)
转自:http://blog.csdn.net/wypblog/article/details/17528851 最近发现自己收集到的Hadoop学习资料有很多本,想想放在那里也浪费,所以觉得贴出来给 ...
- MyBatis Generator 学习
根据数据库,自动生成 VO.XML或者DAO的工具. 同大多数工具(或者框架)一样,需要加载一个配置文件,然后根据配置文件中的内容连接数据库,访问其中的表内容,最后生成实体类以及MAPPER. 占位用 ...
- UCASE() 函数
UCASE() 函数 UCASE 函数把字段的值转换为大写. SQL UCASE() 语法 SELECT UCASE(column_name) FROM table_name
- linux -- Ubuntu14.04及之后版本重启网卡不生效
Ubuntu14.04修改配置,重启网卡没有生效,出现如下问题: service networking restart //重启网络服务 stop: Job failed while stopping ...
- 【BZOJ】1064: [Noi2008]假面舞会(判环+gcd+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1064 表示想到某一种情况就不敢写下去了.... 就是找环的gcd...好可怕.. 于是膜拜了题解.. ...
- poj 3348:Cows(计算几何,求凸包面积)
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6199 Accepted: 2822 Description ...