HDU - 4300(扩展kmp 求后缀的最大前缀)

 

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

题意:

首先t组输入

第一行给你26个字符,即字符串S1, 表示字符的加密方式,即a~z中的第i个字符加密后变为为S1[i]

第二行一个截取的字符串,这个字符串的前部分是情报的密文,后面是情报明文的一部分(因为发送情报的人发现自己被侦察了就不发了),

让你求这个最短的完整信息

 

重点:

密文的长度一定满足temp>=(len+1)/2  这样才符合题意 ,

将该信息解密后的前缀解密后的后缀最大匹配  表示最长的可能明文,注意这个最长的可能明文长度不能超过len-temp

问题就改成了两个字符串求前缀后缀最大匹配(扩展kmp)

 

#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=2e5+7;
typedef long long ll;
const int MOD=1e5+7;
int code[100];//将密文转化为明文
char convert(char c)
{
    return code[c-'a']+'a';
}
char infostr[maxn];
char codestr[maxn];
int net[maxn];//存放info的next数组
int maxpp[maxn];//最长后缀匹配前缀数组
void get_nextval(char *s,int* net)
{
    int ls=strlen(s);
    int j=0,k=-1;
    net[0]=-1;
    while(j<ls)
    {
        if(k==-1||s[j]==s[k])
            net[++j]=net[++k];
        else
            k=net[k];
    }
}
int exkmp(char *s,char *p,int *net)
{
    int ls=strlen(s);
    int temp=(ls+1)/2;//代表最大结尾的匹配不能超过
    get_nextval(p,net);
    int i=temp,k=0;
    /*
    重点:  文本串的第temp+1个字符与模式串的第1个字符开始匹配 因为前temp字符肯定是密文,不能让
    前缀与密文匹配到 ,这样匹配得到的前缀才肯定全都是密文的
    扩展kmp:  计算此串的最大匹配前缀时,利用上一个串的最大匹配前缀
    */
    while(i<ls)
    {
        if(k==-1||s[i]==p[k])//第i+1个字符与第k+1个字符是否相等
        {
             ++i;
             ++k;//代表第i+1个字符的最大匹配为k+1个
             maxpp[i]=k;
        }
        else
            k=net[k];
    }
    return maxpp[ls];
}
int main()
{
    int t,ans;
    char bm[30];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",bm);
        for(int i=0;i<26;++i)
            code[bm[i]-'a']=i;
        scanf("%s",infostr);
        int len=strlen(infostr);
        for(int i=0;i<len;++i)
            codestr[i]=convert(infostr[i]);
        ans=exkmp(infostr,codestr,net);
        printf("%s",infostr);
        for(int i=ans;i<len-ans;++i)
            printf("%c",convert(infostr[i]));
        printf("\n");
    }
}

 

全部评论

相关推荐

最终还是婉拒了小红书的offer,厚着脸皮回了字节。其实这次字节不管是组内的氛围、HR的沟通体验,都比之前好太多,开的薪资也还算过得去,这些都是让我下定决心的原因之一。但最核心的,还是抵不住对Agent的兴趣,选择了Ai&nbsp;Coding这么一个方向。因为很多大佬讲过,在未来比较火的还是属于那些更加垂类的Agent,而Ai&nbsp;Coding恰好是Coding&nbsp;Agent这么一个领域,本质上还是程序员群体和泛程序员群体这个圈子的。目前也已经在提前实习,也是全栈这么一个岗位。就像最近阿里P10针对前端后端等等不再那么区分,确实在Agent方向不太区分这个。尤其是我们自己做AI&nbsp;Coding的内容,基本上90%左右的内容都是AI生成的,AI代码仓库贡献率也是我们的指标之一。有人说他不好用,那肯定是用的姿态不太对。基本上用对Skill、Rules&nbsp;加上比较好的大模型基本都能Cover你的大部分需求,更别说Claude、Cursor这种目前看来Top水准的Coding工具了(叠甲:起码在我看来是这样)。所以不太区分的主要原因,还是针对一些例如Claude&nbsp;Code、Cursor、Trae、Codex、CC等一大堆,他们有很多新的概念和架构提出,我们往往需要快速验证(MVP版本)来看效果。而全栈就是这么快速验证的一个手段,加上Ai&nbsp;Coding的辅助,目前看起来问题不大(仅仅针对Agent而言)。而且Coding的产品形态往往是一个Plugin、Cli之类的,本质还是属于大前端领域。不过针对业务后端来看,区分还是有必要的。大家很多人也说Agent不就是Prompt提示词工程么?是的没错,本质上还是提示词。不过现在也衍生出一个新的Context&nbsp;Eneering,抽象成一种架构思想(类比框架、或者你们业务架构,参考商品有商品发布架构来提效)。本质还是提示词,但是就是能否最大化利用整个上下文窗口来提升效果,这个还是有很多探索空间和玩法的,例如Cursor的思想:上下文万物皆文件,&nbsp;CoWork之类的。后续也有一些Ralph&nbsp;Loop啥的,还有Coding里面的Coding&nbsp;Act姿态。这种才是比较核心的点,而不是你让AI生成的那提示词,然后调用了一下大模型那么简单;也不是dify、LangGraph搭建了一套workflow,从一个node走到另外一个node那么简单。Agent和WorkFLow还是两回事,大部分人也没能很好的区分这一点。不过很多人说AI泡沫啥啥啥的,我们ld也常把这句话挂在嘴边:“说AI泡沫还是太大了”诸如此类。我觉得在AI的时代,懂一点还是会好一点,所以润去字节了。目前的实习生活呢,除了修一些Tools的问题,还包括对比Claude、Cursor、Trae在某些源码实现思想上的点,看看能不能迁移过来,感觉还是比较有意思。不过目前组内还是主要Follow比较多,希望下一个阶段就做一些更有创新的事情哈哈。这就是一个牛马大学生的最终牧场,希望能好好的吧。说不定下次发的时候,正式AI泡沫结束,然后我又回归传统后端这么一个结局了。欢迎交流👏,有不对的🙅不要骂博主(浅薄的认知),可以私聊交流
码农索隆:和优秀的人,做有挑战的事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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