题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4162
题意:
求给定字符的一阶差分链的最小表示。
题解:
先求一阶差分链,再求一阶差分链的最小表示法。
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 3e5 + 10; char s1[maxn],s2[maxn]; int solve(char *s) { int i = 0, j = 1, k = 0,len=strlen(s); while (i < len&&j < len&&k<len) { int 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; } } return i < j ? i : j; } int main() { while (scanf("%s", s1) == 1) { int len = strlen(s1); for (int i = 0; i < len; i++) { s2[i] = (s1[(i + 1) % len] - s1[i] + 8) % 8 + ‘0‘; } s2[len] = ‘\0‘; //cout << s2 << endl; int pos = solve(s2); for (int i = 0; i < len; i++) { printf("%c", s2[(pos + i) % len]); } printf("\n"); } return 0; }
时间: 2024-11-02 12:01:13