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("
");
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]='';
55         kmp();
56     }
57     return 0;
58 }

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

原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/9290085.html