UVA 719 - Glass Beads (最小表示法)

来源:互联网 发布:淘宝宝贝详情怎么做的 编辑:程序博客网 时间:2024/06/09 12:54

UVA 719 - Glass Beads

题目链接

题意:就是求最小表示法的位置

思路:最小表示法裸题

代码:

#include <cstdio>#include <cstring>const int N = 10005;int t;char str[N];int solve(char *str) {    int i, j, l, n = strlen(str);    i = 0; j = 1;    while (i < n && j < n) {for (l = 0; l < n; l++)    if (str[(i + l) % n] != str[(j + l) % n]) break;if (l >= n) break;if (str[(i + l) % n] > str[(j + l) % n])    i = i + l + 1;else    j = j + l + 1;if (i == j) j = i + 1;    }    if (i < j) return i;    return j;}int main() {    scanf("%d", &t);    while (t--) {scanf("%s", str);printf("%d\n", solve(str) + 1);    }    return 0;}


0 0