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 ...
随机推荐
- JAVA多线程编程——JAVA内存模型
一.何为“内存模型” 内存模型描述了程序中各个变量(实例域.静态域和数组元素)之间的关系,以及在实际计算机系统中将变量存储到内存和从内存中取出变量这样的底层细节,对象最终是存储在内存里面的,但是编译器 ...
- python3.6.2利用pyinstaller发布EXE
我的环境是Ubuntu 16.04,系统自带Python2和Python3 安装 pip3 install pyinstaller 发布exe pyinstaller -F helloworld.py ...
- 文件 MD5 SHA1 SHA256 SHA512 校验码生成工具 V1.3
[程序介绍]免费开源的 文件 MD5 SHA1 SHA256 SHA512 校验码生成工具 V1.3 这是一个有意思的程序,同一个程序,即是图形程序,又是命令行程序.程序作用:输入一个文件的路径,输出 ...
- async/await的使用以及注意事项
使用 async / await, 搭配 promise, 可以通过编写形似同步的代码来处理异步流程, 提高代码的简洁性和可读性. 本文介绍 async / await 的基本用法和一些注意事项. a ...
- 10分钟搞懂toString和valueOf函数(详细版)
首先要说明的是这两种方法是toPrimitive抽象操作里会经常用到的. 默认情况下,执行这个抽象操作时会先执行valueOf方法,如果返回的不是原始值,会继续执行toString方法,如果返回的还不 ...
- Maven:项目结构
目录结构图: project |- src |- main //工程源代码目录 |- java //工程java源代 ...
- 音频框架TheAmazingAudioEngine实现音效
TheAmazingAudioEngine是Michael Tyson开源的iOS第三方音频框架.很多音频类APP应用这个框架作开发. 应用这个框架,可以比较方便地实现iOS音频开发中的各种音效的实现 ...
- matplotlib绘图(四)
控制文字属性的方法: 所有的方法都会返回一个matplotlib.text.Text对象 文本注释: annnotate() xy参数设置箭头指示的位置,xytext参数设置注释文字的位置 arro ...
- 单行代码实现xml转换成数组
$string = '<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_m ...
- Linux基础学习-网络管理
Linux系统网络管理NetworkManager 1 启动网络管理服务和开机自启动 在rhel7中网路管理相关命令nmcli,nmtui,nmtui-edit,nm-connection-edito ...