UVA 11452 Dancing the Cheeky-Cheeky

UVA 11452 "Dancing the Cheeky-Cheeky"

找循环节那里要从后往前找,以解决112234112234这样的数据

#include <bits/stdc++.h>

using namespace std;

const int maxn = 10000;
char a[maxn];
int pi[maxn];

void getnext(int n) {
    pi[1] = 0;
    for (int i = 2, j = 0; i <= n; i++) {
        while (j > 0 && a[i] != a[j + 1]) j = pi[j];
        if (a[i] == a[j + 1]) j++;
        pi[i] = j;
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    int _;
    scanf("%d", &_);
    while (_--) {
        scanf("%s", a + 1);
        int n = strlen(a + 1);
        getnext(n);
        int len;
        for (int i = n; i >= 1; i--) {
            if (pi[i] != 0 && i % (i - pi[i]) == 0) {
                len = i - pi[i];
                break;
            }
        }
        int s = n % len;
        int cnt = 0;

        for (int i = s + 1; cnt < 8; i++, cnt++) {
            if (i > len) i = 1;
            printf("%c", a[i]);
        }
        printf("...\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/albert-biu/p/11299706.html
今日推荐