码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11452 Dancing the Cheeky-Cheeky

时间:2019-08-04 21:12:31      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:std   using   ace   printf   problem   har   max   include   index   

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;
}

UVA 11452 Dancing the Cheeky-Cheeky

标签:std   using   ace   printf   problem   har   max   include   index   

原文地址:https://www.cnblogs.com/albert-biu/p/11299706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!