HDU——4162Shape Number(字符串的最小表示)
Shape Number
Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1345 Accepted Submission(s): 647

Two chain codes may represent the same shape if the shape has been rotated, or if a different starting point is chosen for the contour. To normalize the code for rotation, we can compute the first difference of the chain code instead. The first difference is
obtained by counting the number of direction changes in counterclockwise direction between consecutive elements in the chain code (the last element is consecutive with the first one). In the above code, the first difference is
00110026202011676122
Finally, to normalize for the starting point, we consider all cyclic rotations of the first difference and choose among them the lexicographically smallest such code. The resulting code is called the shape number.
00110026202011676122
01100262020116761220
11002620201167612200
...
20011002620201167612
In this case, 00110026202011676122 is the shape number of the shape above.
itself and needs not trace back to the starting point.
12075602223444646600
00110026202011676122
题意很难读懂,看了DISCUSS才知道。
既然懂了题意就好办。偷懒用string超时,改成char数组才AC。
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const int N=300010;
char s[N],temp[N],ans[2*N];
inline int minp(char s[])
{
int i=0,j=1,k=0,t;
int l=strlen(s);
while (i<l&&j<l&&k<l)
{
t=s[(i+k)%l]-s[(j+k)%l];
if(!t)
k++;
else
{
if(t>0)
i+=k+1;
else
j+=k+1;
k=0;
if(i==j)
j++;
}
}
return min(i,j);
}
int main(void)
{
int i,j;
while (~scanf("%s",s))
{
int len=strlen(s);
for (i=0; i<len-1; i++)
{
if(s[i+1]-s[i]>=0)
temp[i]=s[i+1]-s[i]+'0';
else
temp[i]=s[i+1]-s[i]+8+'0';
}
if(s[0]-s[len-1]>=0)
temp[len-1]=temp[len-1]+s[0]-s[len-1]+'0';
else
temp[len-1]=temp[len-1]+s[0]-s[len-1]+8+'0';
temp[len]='\0';
int index=minp(temp);
strcat(ans,temp);
strcat(ans,temp);
for (i=index; i<len+index; i++)
{
putchar(ans[i]);
}
putchar('\n');
memset(temp,0,sizeof(temp));
memset(ans,0,sizeof(ans));
}
return 0;
}
HDU——4162Shape Number(字符串的最小表示)的更多相关文章
- HDU 3374 exkmp+字符串最大最小表示法
题意 找到一个字符串中最先出现的最小(大)表示位置,和最小(大)表示串出现次数 分析 用最小(大)表示法求出最先出现的最小(大)表示位置,然后将串长扩两倍用exkmp找出现次数. Code #incl ...
- kmp的next数组的运用(求字符串的最小循环节)
hdu3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- hdu 5510 Bazinga(字符串kmp)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 6311 Cover (无向图最小路径覆盖)
HDU 6311 Cover (无向图最小路径覆盖) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- POJ 2406 Power Strings(字符串的最小循环节)
题目链接:http://poj.org/problem?id=2406 题意:确定字符串最多是多少个相同的字串重复连接而成的 思路:关键是找到字符串的最小循环节 code: #include < ...
- HDU 1274 展开字符串 (递归+string类)
题目链接:HDU 1274 展开字符串 中文题. 左括号进入DFS函数,右括号return到上一层. 注意return回去的是这个一层递归中的括号中的字母串. AC代码: #include<st ...
- hdu 3374 String Problem(kmp+最小表示法)
Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...
随机推荐
- UVA439 knightMoves (A*启发搜索)
第一个A*,纪念下. A*要保证最短路一定要估价函数小于等于实际值,越接近越好 估价函数取Manhattan距离除以二. //Rey #include<cstdio> #include&l ...
- [web笔记]解决跨域问题以及axios每次提交session变化的问题
- xml文件读取
xml文件如下: <annotation> <folder>bnrc</folder> <filename>jena_000000_000019_lef ...
- CVE-2014-1767
[0x00].简介 CVE-2014-1767漏洞是由于Windows的afd.sys驱动在对系统内存的管理操作中,存在着悬垂指针的问题.在特定情况下攻击者可以通过该悬垂指针造成内存的double ...
- iOS开发遇到的坑之五--解决工程已存在plist表,数据却不能存入的问题
想写这篇博客其实在一两个月前开发遇见的时候就想把这个问题写成博客的,奈何自己一直懒外加一直没有时间,就把这个事情给耽搁了,好在当时知道下自己一定要把这个问题给描述出来,免得以后其他人遇到这个问题会纠结 ...
- 解决cocos游戏安卓release版本闪退问题
在cocos中偶尔会遇到闪退的问题,特别是android和ios系统下的闪退就特别难处理了, 虽然说能使用xcode和eclipse显示log,但是也会出现一些特别的情况,直接闪退而且 没有任何预兆. ...
- python中的sort、sorted排序
我们通常会遇到对数据库中的数据进行排序的问题,今天学习一下对列表和字典的排序方法. 列表 第一种:内建方法sort sort()对列表排序是永久性的排序. 用法:sort(*, key=None, r ...
- Unity基础-图形渲染
图形渲染-Camera Camera下的Clear Flags:Skybox,Don't Clear,Depth only(深度),Solid Color(固定颜色) Culling Mask:渲染层 ...
- NodeJS基础-Buffer
Buffer用于处理二进制数据流 实例类似于整数数组,大小固定 C++代码在V8堆外分配物理内存 // 创建一个长度为10,且用0填充的Buffer const buf1 = Buffer.alloc ...
- 03等待多个线程返回WaitForMultipleObject
二. WaitForMultipleObject 等待单个线程返回 1. 函数原型 DWORD WINAPI WaitForMultipleObjects( _In_ DWORD nCount, _I ...