hdu4300 Clairewd’s message 扩展KMP

B - Clairewd’s message(EKMP)
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

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. 
 

Input

The first line contains only one integer T, which is the number of test cases. 
Each test case contains two lines. The first line of each test case is the conversion table S. S[i] is the ith latin letter's cryptographic letter. The second line is the intercepted text which has n letters that you should recover. It is possible that the text is complete. 
Hint
Range of test data: 
T<= 100 ; 
n<= 100000; 
 

Output

For each test case, output one line contains the shorest possible complete text.
 

Sample Input

     
     
2 abcdefghijklmnopqrstuvwxyz abcdab qwertyuiopasdfghjklzxcvbnm qwertabcde
 

Sample Output

     
     
abcdabcd qwertabcde
 

Hint

Range of test data: 
        
T<= 100 ; 
        
n<= 100000; 
        

    
    
 


这道题有一个很操蛋的地方,就是你读一遍之后根本不知道它在说什么。鉴于智商问题,我读了七八遍之后仍然不能理解,最终百度的,不过,其中GFW的梗还挺好笑。。。


题目大意:给出一个加密字典,即一个字母映射为另一个字母,类似于函数关系;给出一个字符串,它是由 完整的加密串+不一定完整的解密串 相连而成,要求将这个串补充完整,即将不完整的部分补充完整,并且这个完整的串还是最短的。


解题思路:1. 将原始串再做一次加密,加密+解密 就会变成 XX+加密

                  2. 设原始串为s(主串),新串为t(模式串),对两串进行扩展KMP运算

  3. 从中间开始扫描extend数组,如果遇到i+extend[i]==len&&i>=extend[i]的情况,表明找到解。


解释:为了找到最短的串,我们要尽量让加密串与解密串一样长,也就是要从中间开始找,如果找到一个解,那么从i到len的串必定全部匹配,i既表示解密串的起始位置,也表示加密串的长度,extend[i]则表示后一半长度,这就是i+extend[i]==len,而且,要保证两部分不重合,这就是i>=extend[i]。


代码


#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX 100010
using namespace std;

void get_next(char x[],int m,int next_[])
{
    next_[0]=m;
    int j=0;
    while(j+1<m&&x[j]==x[j+1]) j++;
    next_[1]=j;
    int k=1;
    for(int i=2; i<m; i++)
    {
        int p=next_[k]+k-1;
        int L=next_[i-k];
        if(i+L<p+1) next_[i]=L;
        else
        {
            j=max(0,p-i+1);
            while(i+j<m&&x[i+j]==x[j]) j++;
            next_[i]=j;
            k=i;
        }
    }
}


void ekmp(char x[],int m,char y[],int n,int next_[],int extend[])
{
    get_next(x,m,next_);
    int j=0;
    while(j<n&&j<m&&x[j]==y[j]) j++;
    extend[0]=j;
    int k=0;
    for(int i=1; i<n; i++)
    {
        int p=extend[k]+k-1;
        int L=next_[i-k];
        if(i+L<p+1) extend[i]=L;
        else
        {
            j=max(0,p-i+1);
            while(i+j<n&&j<m&&y[i+j]==x[j]) j++;
            extend[i]=j;
            k=i;
        }
    }
}




char s[MAX],t[MAX],dic[27];
char dc[200];
int len;
int Next[MAX],extend[MAX];


int main()
{
    int T,i,j,bg;
    char tmp;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s%s",dic,t);
        len=strlen(t);

        for(i=0;dic[i]!='\0';i++)//计算解密字典
        {
            dc[dic[i]-'a']='a'+i;
        }

        for(i=0; i<len; i++)//二次加密
        {
            s[i]=dic[t[i]-'a'];
        }
        ekmp(t,len,s,len,Next,extend);
        for(i=len/2; i<len; i++)
        {
            if(i+extend[i]==len && i>=extend[i])//重要,需要长度相等且不重合
                break;
        }
        for(j=0; j<i; j++)
            printf("%c",t[j]);
        for(j=0; j<i; j++)//输出解密内容
            printf("%c",dc[t[j]-'a']);
        printf("\n");
    }
    return 0;
}





深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值