题目链接: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. chrome 插件地址 知乎

    chrome运行内存过大:https://www.zhihu.com/question/20061319 chrome扩展程序:https://www.zhihu.com/question/19594 ...

  2. web应用中幂等性的学习

    qnmd bd:待会劳资就去买个vpn. 在平常的工作中经常听到也用到幂等,却没有及时学习总结这个知识点,现在到时候了. 幂等性最初是一个数学上的概念:在某二元运算下,幂等元素是指被自己重复运算(或对 ...

  3. C语言 · 利息计算

    算法提高 利息计算   时间限制:1.0s   内存限制:512.0MB      编制程序完成下述任务:接受两个数,一个为用户一年期定期存款金额,一个为按照百分比格式表示的利率:程序计算一年期满 后 ...

  4. [Linux]Linux应用程序中添加强制中断处理

    注册Ctrl+C的按键signal信号捕捉,在捕捉到该动作后,强制退出应用程序 void handle_sig(int num) { printf( "%s\n", __func_ ...

  5. HashMap与ConcurrentHashMap的区别(转)

    从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心. 在JDK1.5中,伟大的Doug Lea给我们带来了concurrent包,从 ...

  6. Supervisor重新加载配置启动新的进程

    一.添加好配置文件后 二.更新新的配置到supervisord supervisorctl update 三.重新启动配置中的所有程序 supervisorctl reload 四.启动某个进程(pr ...

  7. python ascii codec can't decode

    提示错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 240: ordinal not in range ...

  8. 二分图匹配 + 最小点覆盖 - Vertex Cover

    Vertex Cover Problem's Link Mean: 给你一个无向图,让你给图中的结点染色,使得:每条边的两个顶点至少有一个顶点被染色.求最少的染色顶点数. analyse: 裸的最小点 ...

  9. GAN 生成mnist数据

    参考资料 GAN原理学习笔记 生成式对抗网络GAN汇总 GAN的理解与TensorFlow的实现 TensorFlow小试牛刀(2):GAN生成手写数字 参考代码之一 #coding=utf-8 #h ...

  10. AWT是Java最早出现的图形界面,但很快就被Swing所取代

    AWT是Java最早出现的图形界面,但很快就被Swing所取代. Swing才是一种真正的图形开发. AWT在不同平台所出现的界面可能有所不同:因为每个OS都有自己的UI组件库,java调用不同系统的 ...