Shape Number

Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1345    Accepted Submission(s): 647

Problem Description
In computer vision, a chain code is a sequence of numbers representing directions when following the contour of an object. For example, the following figure shows the contour represented by the chain code 22234446466001207560 (starting at the upper-left corner).


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.

 
Input
The input consists of a number of cases. The input of each case is given in one line, consisting of a chain code of a shape. The length of the chain code is at most 300,000, and all digits in the code are between 0 and 7 inclusive. The contour may intersect
itself and needs not trace back to the starting point.
 
Output
For each case, print the resulting shape number after the normalizations discussed above are performed.
 
Sample Input
22234446466001207560
12075602223444646600
 
Sample Output
00110026202011676122
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(字符串的最小表示)的更多相关文章

  1. HDU 3374 exkmp+字符串最大最小表示法

    题意 找到一个字符串中最先出现的最小(大)表示位置,和最小(大)表示串出现次数 分析 用最小(大)表示法求出最先出现的最小(大)表示位置,然后将串长扩两倍用exkmp找出现次数. Code #incl ...

  2. kmp的next数组的运用(求字符串的最小循环节)

    hdu3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  3. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  4. hdu 5510 Bazinga(字符串kmp)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  5. HDU 6311 Cover (无向图最小路径覆盖)

    HDU 6311 Cover (无向图最小路径覆盖) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...

  6. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  7. POJ 2406 Power Strings(字符串的最小循环节)

    题目链接:http://poj.org/problem?id=2406 题意:确定字符串最多是多少个相同的字串重复连接而成的 思路:关键是找到字符串的最小循环节 code: #include < ...

  8. HDU 1274 展开字符串 (递归+string类)

    题目链接:HDU 1274 展开字符串 中文题. 左括号进入DFS函数,右括号return到上一层. 注意return回去的是这个一层递归中的括号中的字母串. AC代码: #include<st ...

  9. hdu 3374 String Problem(kmp+最小表示法)

    Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...

随机推荐

  1. CF Gym 100187D Holidays (数学,递推)

    题意:给n个元素,从n中选两个非空集合A和B.问有多少中选法? 递推: dp[n]表示元素个数为n的方案数,对于新来的一个元素,要么加入集合,要么不加入集合自成一个集合.加入集合有三种选择,A,B,E ...

  2. Python 继承、派生、组合、接口、抽象类

    继承是一种是的关系,和组合对比,组合是一种有的关系,这两者都是解决代用重用问题的 继承 注意:继承不是遗传,在显示角度中,是通过对象抽象成类,再把这些类抽象成一个,就是父类.是自下而上的过程,在程序中 ...

  3. c++作业:递归调用,例题4.5 求第五个人的年龄

    递归调用,例题4.5 求第五个人的年龄 #include <iostream> using namespace std; int age(int num){ int a; ) a=; el ...

  4. Java 的Throwable、error、exception的区别

    1. 什么是异常? 异常本质上是程序上的错误,包括程序逻辑错误和系统错误.比如使用空的引用(NullPointerException).数组下标越界(IndexOutOfBoundsException ...

  5. 【前端_js】Json对象和Json字符串的区别

    转载1: Json对象和Json字符串的区别 转载2: JSON字符串与JSON对象的区别

  6. 201621123080《Java程序设计》第1周学习总结

    作业01-Java基本概念 1. 本周学习总结 关键词: JDK.JAVA.编程.基础语法 概念之间的关系: JDK是JAVA的开发工具,学习JAVA的主要方法是大量编程,语法是JAVA的基础 2. ...

  7. Unity基础-图形渲染

    图形渲染-Camera Camera下的Clear Flags:Skybox,Don't Clear,Depth only(深度),Solid Color(固定颜色) Culling Mask:渲染层 ...

  8. 快速排序和快速选择(quickSort and quickSelect)算法

    排序算法:快速排序(quicksort)递归与非递归算法 TopK问题:快速选择(quickSelect)算法 import java.util.*; import java.lang.*; publ ...

  9. 【js】window.onscroll 无效问题

    body 设置为height:100% 导致window.onscroll 无效

  10. 20181205(模块循环导入解决方案,json&pickle模块,time,date,random介绍)

    一.补充内容 循环导入 解决方案: 1.将导入的语句挪到后面. ​ 2.将导入语句放入函数,函数在定义阶段不运行 #m1.pyprint('正在导入m1')   #②能够正常打印from m2 imp ...