hdu 4300

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300

 

题意:第一行为密码表,第二行包含暗文和明文。

qwertyuiopasdfghjklzxcvbnm

qwertabcde

第二行的q在字母表里面是第17位,对应的第一行里面的第十七位j。则转换为明文的为j。

明文不一定全都出现在第二行中,所以,遇到不全的要补齐。

 1 #include<cstdio>
 2 #include<map>
 3 #include<cstring>
 4 using namespace std;
 5 const int maxn=100006;
 6 char a[200],b[maxn],c[maxn];
 7 int nex[maxn];
 8 int n;
 9 
10 void get_next()
11 {
12     int temp=0;
13     for(int i=1;i<n;i++){
14         while(temp>0&&c[temp]!=c[i])
15             temp=nex[temp-1];
16         if(c[i]==c[temp])
17             temp++;
18         nex[i]=temp;
19     }
20 }
21 
22 void kmp()
23 {
24     get_next();
25     int temp=0;
26     for(int i=0;i<n;i++){
27         while(temp>0&&c[temp]!=b[i])
28             temp=nex[temp-1];
29         if(c[temp]==b[i])
30             temp++;
31     }
32     while(temp*2>n)
33         temp=nex[temp-1];
34     printf("%s",b);
35     for(int i=temp;i<n-temp;i++)
36         printf("%c",c[i]);
37     printf("\n");
38 }
39 
40 int main()
41 {
42     int T;
43     scanf("%d",&T);
44     while(T--){
45         scanf("%s%s",a,b);
46         map<char,char> op;
47         op.clear();
48         int len=strlen(a);
49         for(int i=0;i<len;i++)
50             op[a[i]]=i+'a';
51         n=strlen(b);
52         for(int i=0;i<n;i++)
53             c[i]=op[b[i]];
54         c[n]='\0';
55         kmp();
56     }
57     return 0;
58 }

这里的kmp偏移了一位,所以有所不同。

转载于:https://www.cnblogs.com/ZQUACM-875180305/p/9290085.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值