题目链接: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(最小表示)的更多相关文章

  1. hdu2609 最小表示法

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  2. hdu2609最小表示法

    #include <iostream> #include <algorithm> #include <string.h> #include <cstdio&g ...

  3. HDU2609 How many —— 最小表示法

    题目链接:https://vjudge.net/problem/HDU-2609 How many Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  4. hdu2609 How many【最小表示法】【Hash】

    How many Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. hdu2609 How many 字典树+最小表示法

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell meHow many ...

  6. hdu2609(最小表示法)

    题意:有n个环形字符串,一个环形字符串移动会形成不能的字符串,我们把它们看作同一串字符串,求有多少个不同的字符串....... 思路:用最小表示发将一个环形串的最小字典序找出来,然后让这个环形串按照这 ...

  7. 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 ...

  8. BZOJ 1391: [Ceoi2008]order [最小割]

    1391: [Ceoi2008]order Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1509  Solved: 460[Submit][Statu ...

  9. 《徐徐道来话Java》:PriorityQueue和最小堆

    在讲解PriorityQueue之前,需要先熟悉一个有序数据结构:最小堆. 最小堆是一种经过排序的完全二叉树,其中任一非终端节点数值均不大于其左孩子和右孩子节点的值. 可以得出结论,如果一棵二叉树满足 ...

  10. C++ 最小化到托盘

    #define WM_SHOWTASK (WM_USER + 1) void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID &a ...

随机推荐

  1. 今天遇到个PHP不知原因的报内部错误

    今天遇到个PHP不知原因的报内部错误 纠结了很久想尽了办法,1.apache日志 2.错误级别 ,还差点就把自己写的那个破烂不堪的日志系统加上去了 纠结了很久还是无果,在最终,最终发现了 原来是类命名 ...

  2. [C++]using std string;的作用是什么

    相关资料: http://bbs.csdn.net/topics/330194465 #include <string>将string库包含到当前编译单元中. using std::str ...

  3. Tablespace for table '`pomelo`.`bag`' exists. Please DISCARD the tablespace before IMPORT.

    //遇到的问题是,删除数据库之后,重新创建数据库,在创建数据库表的时候,明明没有该表,却提示存在这个表.这是数据库缓存造成的 //解决方法 FLUSH TABLES; /* 安装MySql数据库(略) ...

  4. Django 1 创建项目

    shell中输入 django-admin.py startproject mysite. 然后进入mysite目录,输入python manage.py runserver 0.0.0.0:8000 ...

  5. [fork]Linux中的fork函数详解

    ---------------------------------------------------------------------------------------------------- ...

  6. hive中关键字作为列名的方法

    hive中有很多关键字,直接作为列名,会出错的 例如 下面 user就是关键字,作为字段时报以下错误. 解决方案: 使用·· (ESC下面的那个键,点号)两个符号包裹即可.

  7. keepalive脑裂的处理,从节点发现访问的虚拟IP就报警,同时尝试发送内容到主节点服务器关闭keepalive和nginx,或者关机

    解决keepalived脑裂问题   检测思路:正常情况下keepalived的VIP地址是在主节点上的,如果在从节点发现了VIP,就设置报警信息 脚本如下: 1 2 3 4 5 6 7 8 9 10 ...

  8. 打开palette控制面板

    (2)

  9. js学习笔记25----Event对象

    Event : 事件对象,当一个事件发生的时候,和当前这个对象发生的这个事件有关的一些详细的信息都会被临时保存到一个指定的地方-event 对象,供我们在需要时调用. 事件对象必须在一个事件调用的函数 ...

  10. METIS 安装过程

    官网下载包 yum -y instll gcc yum -y install gcc* yum -y install cmake 环境Python2.7.3 创建/home/Python/metis ...