您的位置:首页 > 其它

Minimum Expression of String 字符串最小表示

2012-09-21 18:33 363 查看
  十分详细的解释:http://hi.baidu.com/kfkkwlhvxcadotr/item/14a46a2b1f889e152a0f1c5f

几道相关例题的代码:

#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <set>
#include <string>

using namespace std;

set<string> cnt;
char buf[300001], tmp[300001];

int minExp(char *s) {
int i = 0, j = 1, k = 0, t;
int len = strlen(s);

while (i < len && j < len && k < len) {
t = s[(i + k) % len] - s[(j + k) % len];
if (!t) {
k++;
} else {
if (t > 0) i = i + k + 1;
else {
j = j + k + 1;
}
if (i == j) j++;
k = 0;
}
//printf("%d %d %d\n", i, j, k);
}

return min(i, j);
}

void work() {
int len = strlen(buf);

tmp[0] = (buf[0] - buf[len - 1] + 8) % 8 + '0';
for (int i = 1; i < len; i++){
tmp[i] = (buf[i] - buf[i - 1] + 8) % 8 + '0';
}

int pos = minExp(tmp);

for (int i = 0; i < len; i++) {
buf[i] = tmp[(pos + i) % len];
}
}

int main() {
while (~scanf("%s", buf)) {
work();
printf("%s\n", buf);
}

return 0;
}


hdu 4162

——written by Lyon
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: