hdu 4333 Revolving Digits(扩展kmp)

6 篇文章 0 订阅

Revolving Digits

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1969    Accepted Submission(s): 565


Problem Description
One day Silence is interested in revolving the digits of a positive integer. In the revolving operation, he can put several last digits to the front of the integer. Of course, he can put all the digits to the front, so he will get the integer itself. For example, he can change 123 into 312, 231 and 123. Now he wanted to know how many different integers he can get that is less than the original integer, how many different integers he can get that is equal to the original integer and how many different integers he can get that is greater than the original integer. We will ensure that the original integer is positive and it has no leading zeros, but if we get an integer with some leading zeros by revolving the digits, we will regard the new integer as it has no leading zeros. For example, if the original integer is 104, we can get 410, 41 and 104.
 

Input
The first line of the input contains an integer T (1<=T<=50) which means the number of test cases.
For each test cases, there is only one line that is the original integer N. we will ensure that N is an positive integer without leading zeros and N is less than 10^100000.
 

Output
For each test case, please output a line which is "Case X: L E G", X means the number of the test case. And L means the number of integers is less than N that we can get by revolving digits. E means the number of integers is equal to N. G means the number of integers is greater than N.
 

Sample Input
  
  
1 341
 

Sample Output
  
  
Case 1: 1 1 1
 
 
题意:给出一个数字串,有右移操作,使这个数字变为另一个数字,问通过右移操作后,有多少个数字小于、等于和大于原来的数字?
思路:扩展kmp。将原串复制成两倍长度,求next数组,扩展kmp的next[i]表示模式串以i开头的后缀与该串前缀的匹配长度。设原来的串的长度为len,利用next数组,对于某个位置i,若next[i] >= len,那么以该位置开头的串与原串匹配长度>=len,可知该串=原串。否则,若str[next[i]] < str[i+next[i]],则原串在i这个位置<该串,可知该串>原串,否则小于原串。另外还要求最小循环节,避免重复计数。
 
 
AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <algorithm>
#define ll long long

using namespace std;

const int INF = 1e9;
const int maxn = 100005;

char str[2 * maxn];
int next[2 * maxn];
void get_next(char *s, int len){
    memset(next, 0, sizeof(next));
    for(int i = 1; i < len; i++)
    {
        int tmp = next[i - 1];
        while(tmp && s[i] != s[tmp]) tmp = next[tmp - 1];
        if(s[tmp] == s[i]) next[i] = tmp + 1;
        else next[i] = 0;
    }
}
void get_extend(char *s, int len){
    int j = 0, k = 1;
    next[0] = len;
    while(s[j] == s[j + 1]) j++;
    next[1] = j;
    for(int i = 2; i < len; i++)
    {
        int Len = k + next[k] - 1, L = next[i - k];
        if(L < Len - i + 1) next[i] = L;
        else
        {
            j = max(0,Len -i +1);
            while(s[i + j] == s[j]) j++;
            next[i] = j, k = i;
        }
    }
}
int main()
{
    int t, ca = 0;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s", str);
        int len = strlen(str);
        get_next(str, len);
        int tmp = len % (len - next[len - 1]) ? 1 : len / (len - next[len - 1]);
        for(int i = len ; i < 2 * len; i++) str[i] = str[i - len];
        str[2 * len] = '\0';
        get_extend(str, 2 * len);
        int a = 0, b = 0, c = 0;
        for(int i = 0; i < len; i++)
        {
            if(next[i] >= len) b++;
            else if(str[next[i]] < str[i + next[i]]) c++;
            else a++;
        }
        printf("Case %d: %d %d %d\n", ++ca, a / tmp, b / tmp, c / tmp);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值