【题解】 NC5157B 密码系统

密码系统

https://ac.nowcoder.com/acm/contest/5157/B

solution

其实本题只要解决一个问题,剩下的就是模拟了。

这个问题就是如何快速的比较两个字符串的字典序。这是一个非常经典的思路。我们对这两个字符串都哈希一遍,然后二分一下这两个字符串最长的公共前缀长度。然后比较最长公共前缀的下一位上的字符就行了。因为所有要比较的字符串都在同一个母串上。我们只要对这个母串进行一遍哈希就行了。

剩下的按照题目中讲的模拟即可。

code

/*
* @Author: wxyww
* @Date:   2020-04-17 19:15:03
* @Last Modified time: 2020-04-17 19:23:46
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1000010;
ll read() {
    ll x = 0,f = 1;char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = -1; c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = x * 10 + c - '0'; c = getchar();
    }
    return x * f;
}
char s[N];

ull hs[N],base[N],B = 28;

int n,K,now[N];
ull get(int l,int r) {
    return hs[r] - hs[l - 1] * base[r - l + 1];
}
int check(int x,int y) {
    int l = 1,r = K,ans = 0;
    while(l <= r) {
        int mid = (l + r) >> 1;
        if(get(x,x + mid - 1) == get(y,y + mid - 1)) ans = mid,l = mid + 1;
        else r = mid - 1;
    }
    return s[x + ans] < s[y + ans]; 
}

int main() {
    n = read(),K = read();
    scanf("%s",s + 1);

    for(int i = n + 1;i <= n + n;++i)
        s[i] = s[i - n];

    base[0] = 1;
    for(int i = 1;i <= n + n;++i) {
        hs[i] = hs[i - 1] * B + s[i] - 'a' + 1;
        base[i] = base[i - 1] * B;
    }

    for(int i = 1;i <= n;++i) {
        if(!now[i % K]) now[i % K] = i;
        else {
            if(check(now[i % K],i)) now[i % K] = i;
        }
    }
    int ans = now[0];
    for(int i = 1;i < K;++i) {
        if(check(now[i],ans)) ans = now[i];
    }


    for(int i = ans;i <= ans + K - 1;++i)
        putchar(s[i]);

    return 0;
}
全部评论

相关推荐

03-26 13:04
已编辑
电子科技大学 算法工程师
xiaowl:你这个简历“条目上”都比较有深度性,但是实际上面试官又没法很好的评估你是怎么达到很多看上去很厉害的结果的。要避免一些看上去很厉害的包装,比如高效的内存复用策略的表达,如果仅是简单的一些内存共享机制,而且面试上也没有深挖的空间,就不要这样表达。比如,工程化模式本质上可能就是定义了一些abstract class,那也就没特别多值得讲的内容。建议简历上应该侧重那些你花了大量时间和精力解决、研究的问题,不要过分追求“丰富”,而是关注在技术深入度、问题解决能力的表现上。
没有实习经历,还有机会进...
点赞 评论 收藏
分享
评论
4
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务