hdu6223 Infinite Fraction Path

Infinite Fraction Path

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2139    Accepted Submission(s): 434


Problem Description
The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and solicited an audience with the king. He recounted how he had built a happy path in the kingdom of happiness. The king affirmed Welly’s talent and hoped that this talent can help him find the best infinite fraction path before the anniversary.
The kingdom has N cities numbered from 0 to N - 1 and you are given an array D[0 ... N - 1] of decimal digits (0 ≤ D[i] ≤ 9, D[i] is an integer). The destination of the only one-way road start from the i-th city is the city labelled ( i2 + 1)%N.
A path beginning from the i-th city would pass through the cities  u1,u2,u3, and so on consecutively. The path constructs a real number A[i], called the relevant fraction such that the integer part of it is equal to zero and its fractional part is an infinite decimal fraction with digits D[i], D[ u1], D[ u2], and so on.
The best infinite fraction path is the one with the largest relevant fraction
 

Input
The input contains multiple test cases and the first line provides an integer up to 100 indicating to the total numberof test cases.
For each test case, the first line contains the integer N (1 ≤ N ≤ 150000). The second line contains an array ofdigits D, given without spaces.
The summation of N is smaller than 2000000.
 

Output
For each test case, you should output the label of the case first. Then you are to output exactly N characters which are the first N digits of the fractional part of the largest relevant fraction.
 

Sample Input
 
 
43149512345732145679261025520
 

Sample Output
 
 
Case #1: 999Case #2: 53123Case #3: 7166666Case #4: 615015015
 

题意:根据给定的等长字符串,输出字典序最大的那个字符串

解法:修改后缀数组模板,然后套模板即可

#include "bits/stdc++.h"
using namespace std;
typedef long long LL;

const int MX = 2e5 + 5;
char s[MX];
int SA[MX], R[MX], H[MX];
int wa[MX], wb[MX], wv[MX], wc[MX];
int nxt[MX],nn[2][MX];
queue <int> que[MX];
bool cmp(int *r, int a, int b, int a2, int b2) {
    return r[a] == r[b] && r[a2] == r[b2];
}
void Suffix(char *r, int m = 128) {
    int n = strlen(r) + 1, cur = 0;
    int i, j, p, *x = wa, *y = wb, *t;
    for(i = 0; i < m; i++) wc[i] = 0;
    for(i = 0; i < n; i++) wc[x[i] = r[i]]++;
    for(i = 1; i < m; i++) wc[i] += wc[i - 1];
    for(i = n - 1; i >= 0; i--) SA[--wc[x[i]]] = i;
    for(j = 1, p = 1; j <= n; j *= 2, m = p) {
        for(i = 0; i < n; i++) que[nn[cur][i]].push(i);
        for(i = 0, p = 0; i < n; i++){
            while(que[SA[i]].size()) {
                y[p++] = que[SA[i]].front();
                que[SA[i]].pop();
            }
        }
        for(i = 0; i < n; i++) wv[i] = x[y[i]];
        for(i = 0; i < m; i++) wc[i] = 0;
        for(i = 0; i < n; i++) wc[wv[i]]++;
        for(i = 1; i < m; i++) wc[i] += wc[i - 1];
        for(i = n - 1; i >= 0; i--) SA[--wc[wv[i]]] = y[i];
        for(t = x, x = y, y = t, p = 1, x[SA[0]] = 0, i = 1; i < n; i++) {
            x[SA[i]] = cmp(y, SA[i - 1], SA[i], nn[cur][SA[i-1]], nn[cur][SA[i]]) ? p - 1 : p++;
        }
        for(i = 0; i < n; i++) nn[cur^1][i] = nn[cur][nn[cur][i]];
        cur ^= 1;
    }
}


int main() {
#ifdef __LOCAL__
    freopen("input.txt","r",stdin);
#endif // __LOCAL__
    int T,n;
    int cas = 0;
    scanf("%d",&T);
    while(T--){
        printf("Case #%d: ",++cas);
        scanf("%d",&n);
        scanf("%s",s);
        for(LL i = 0; i < n; i++){
            nxt[i] = (LL) (i*i+1)%n;
            nn[0][i] = nxt[i];
        }
        nxt[n] = nn[0][n] = n;
        Suffix(s);

        for(int i = 0, pos = SA[n]; i < n; i++, pos = nxt[pos])
            printf("%c",s[pos]);
        puts("");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值