您的位置:首页 > 其它

HDU——4162Shape Number(字符串的最小表示)

2016-05-15 00:22 288 查看


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
,temp
,ans[2*N];
inline int minp(char s[])
{
int i=0,j=1,k=0;
int l=strlen(s);
while (1)
{
if(j+k>=l||i+k>=l)
break;
if(s[i+k]==s[j+k])
{
k++;
continue;
}
else
{
if(s[i+k]>s[j+k])
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: