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. C、C++、C#中struct的简单比较

    比较这三者是因为C.C++.C#这三者存在着一定的血缘关系,而他们三者都有的struct却有很大的不同. 功能 C中的struct是最简单的.只能有成员变量而不能有成员函数. C++和C#中都支持,而 ...

  2. 2018.3.16 win 关闭自动更新代码

    新建一个文本文件修改后缀名为 .bat 格式 net stop wuauserv sc config wuauserv start=disabled shutdown -r -t 1

  3. 两个对象值转换的方法(BeanUtils.copyProperties与JSONObject.parseObject对比)

    将源对象赋值到目标对象方法: 方法一:BeanUtils.copyProperties(源对象, 目标对象); //org.springframework.beans.BeanUtils 方法二:目标 ...

  4. FastText算法

    转载自: https://www.cnblogs.com/huangyc/p/9768872.html 0. 目录 1. 前言 2. FastText原理 2.1 模型架构 2.2 层次SoftMax ...

  5. 《剑指offer》56 数组中只出现一次的数字

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.   在线练习:https://www.nowcoder.com/practice/e02fdb5 ...

  6. d3.js--04(enter和exit)

    enter() 当DOM数量少于data的数量,或者压根一个都没有的时候,我们一般会希望让程序帮忙创建. <!DOCTYPE html> <html> <head> ...

  7. [vijos]P1514 天才的记忆

    背景 神仙飞啊飞 描述 从前有个人名叫W and N and B,他有着天才般的记忆力,他珍藏了许多许多的宝藏.在他离世之后留给后人一个难题(专门考验记忆力的啊!),如果谁能轻松回答出这个问题,便可以 ...

  8. [BZOJ] 1563: [NOI2009]诗人小G

    1D/1D的方程,代价函数是一个p次函数,典型的决策单调性 用单调队列(其实算单调栈)维护决策点,优化转移 复杂度\(O(nlogn)\) #include<iostream> #incl ...

  9. Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决

    大意是Log4jConfigListener在获取webapp.root值时,被后一context的值替换掉了,所以要在各个项目的web.xml中配置不同的webAppRootKey值,随即在其中一个 ...

  10. Library setup