其他分享
首页 > 其他分享> > HDU-4300-Clairewd's message(扩展KMP)

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

标签:HDU,4300,int,Clairewd,len,Next,++,MAXN,include
来源: https://www.cnblogs.com/YDDDD/p/11593992.html