HDU-4300-Clairewd's message(扩展KMP)

链接:

https://vjudge.net/problem/HDU-4300

题意:

Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important messages and she was preparing for sending it to ykwd. They had agreed that each letter of these messages would be transfered to another one according to a conversion table.
Unfortunately, GFW(someone's name, not what you just think about) has detected their action. He also got their conversion table by some unknown methods before. Clairewd was so clever and vigilant that when she realized that somebody was monitoring their action, she just stopped transmitting messages.
But GFW knows that Clairewd would always firstly send the ciphertext and then plaintext(Note that they won't overlap each other). But he doesn't know how to separate the text because he has no idea about the whole message. However, he thinks that recovering the shortest possible text is not a hard task for you.
Now GFW will give you the intercepted text and the conversion table. You should help him work out this problem.

思路:

题意太难懂. 简单来说, 就是先给一个字符表, 然后给一个由密文和明文组成的字符串,
但是可能不全, 求组成密文和明文最小的长度.
给字符串解密后, 跑扩展KMP, 找一个密文后缀, 明文前缀.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;
const int MOD = 1e4+7;

char S[MAXN], a[MAXN], b[MAXN], to[MAXN];
int Next[MAXN], Exten[MAXN];

void GetNext(char *s)
{
    int len = strlen(s);
    int a = 0, p = 0;
    Next[0] = len;
    for (int i = 1;i < len;i++)
    {
        if (i >= p || i+Next[i-a] >= p)
        {
            if (i >= p)
                p = i;
            while (p < len && s[p] == s[p-i])
                p++;
            Next[i] = p-i;
            a = i;
        }
        else
            Next[i] = Next[i-a];
    }
}

void ExKmp(char *s, char *t)
{
    int len = strlen(s);
    int a = 0, p = 0;
    GetNext(t);
    for (int i = 0;i < len;i++)
    {
        if (i >= p || i + Next[i-a] >= p)
        {
            if (i >= p)
                p = i;
            while (p < len && s[p] == t[p-i])
                p++;
            Exten[i] = p-i;
            a = i;
        }
        else
            Exten[i] = Next[i-a];
    }

}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%s", S);
        scanf("%s", a);
        int lens = strlen(S), lena = strlen(a);
        for (int i = 0;i < lens;i++)
            to[S[i]-'a'] = 'a'+i;
        for (int i = 0;i < lena;i++)
            b[i] = to[a[i]-'a'];
        b[lena] = 0;
        ExKmp(a, b);
        int len = strlen(a);
        int p = len;
        for (int i = 0;i < len;i++)
        {
            if (i+Exten[i] >= len && i >= Exten[i])
            {
                p = i;
                break;
            }
        }
        a[p] = 0;
        strcpy(b, a);
        for (int i = 0;i < p;i++)
            b[p+i] = to[b[i]-'a'];
        b[2*p] = 0;
        printf("%s\n", b);
    }

    return 0;
}

转载于:https://www.cnblogs.com/YDDDD/p/11593992.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值