1.题目描述:
John likes simple ciphers. He had been using the “Caesar” cipher to encrypt his diary until recently, when he learned a hard lesson about its strength by catching his sister Mary browsing through the diary without any problems.
Rapidly searching for an alternative, John found a solution: the famous “Autokey” cipher. He uses a version that takes the 26 lower-case letters ‘a’–‘z’ and internally translates them in alphabetical order to the numbers 0 to 25.
The encryption key k begins with a secret prefix of n letters. Each of the remaining letters of the key is copied from the letters of the plaintext a, so that kn+i=ai for i≥1. Encryption of the plaintext a to the ciphertext b follows the formula bi=ai+kimod26.
Mary is not easily discouraged. She was able to get a peek at the last n letters John typed into his diary on the family computer before he noticed her, quickly encrypted the text document with a click, and left. This could be her chance.
Input
The input consists of:
One line with two integers n and m (1≤n≤30, n+1≤m≤100), where n is the length of the keyword as well as the number of letters Mary saw, and m is the length of the text.
One line with n lower-case letters, the last n letters of the plaintext.
One line with m lower-case letters, the whole ciphertext.
Output
Output the plaintext of John’s diary.
Examples
Input
5 16
again
pirpumsemoystoal
Output
marywasnosyagain
Input
1 12
d
fzvfkdocukfu
Output
shortkeyword
2.题意:
给出以前n个位神秘(无用)前缀,往后复制明文a串的秘钥,得到了密文b,现在知道后n位的明文和所有的密文,求原明文。
3.思路:
签到题啦(啪——)。思路很简单,对于密文b有:bi =(ki+ai)(mod 26),考虑bm=(km+am)%26 又知道K后m-n位都为a的前缀,那么km=am-n,可以得到递推式子:am-n=(bm-am+26)%26+‘a’;
4.代码:
//K - Kleptography
//#include
//#pragma GCC optimize(3,"Ofast","inline")
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#include
#include
#include
#include
#include
#define FAST ios::sync_with_stdio(false)
#define DEV_RND ((int)rand()*RAND_MAX+rand())
#define RND(L,R) (DEV_RND%((R)-(L)+1)+(L))
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i
#define repn(i,a,n,t) for(int i=a;i
#define per(i,n,a) for(int i=n-1;i>=a;--i)
#define pern(i,n,a,t) for(int i=n-1;i>=a;i-=t)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define li inline
#define re register
using namespace std;
//typedef uniform_int_distribution RNDI;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef double db;
typedef long long ll;
typedef long double ld;
const int maxn = 1e5+5;
const int maxm = 100000+5;
const int inf=0x3f3f3f3f;
const double eps = 1e-9;
const double pi=acos(-1);
const int mod = 1e9+7;
//int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
//li int f(int x){return x==par[x]?par[x]:par[x]=f(par[x]);}
//mt19937 eng(time(0));
li ll lowbit(ll x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//li int RND(int L,int R){RNDI rnd(L,R);return rnd(eng);}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
li ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
li ll qmul(ll a,ll b,ll MOD=mod){return (a*b-(ll)((long double)a/MOD*b)*MOD+MOD)%MOD;}
li ll Qpow(ll a,ll b,ll MOD){ll res=1;while(b>0){if(b&1) res=qmul(res,a,MOD);a=qmul(a,a,MOD);b>>=1;}return res;}
li ll invp(ll x,ll p){return qpow(x,p-2,p);}
ll invd(ll x,ll p){ll res,d,t;ex_gcd(x,p,d,res,t);return res;}
li ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
li void debug(){ofstream fout("C:\\Users\\Administrator\\Desktop\\in.txt");fout.close();}
namespace IO
{
li int read()
{
int x=0,sign=1;char c=getchar();
while(c>'9'||c<'0') {if(c=='-') sign=-1;c=getchar();}
while('0'<=c&&c<='9') x=x*10+c-'0',c=getchar();
return x*sign;
}
template<typename T>
li void write(T x,char t='\n')
{
if(x<0){x=-x;putchar('-');};
static int sta[25];int top=0;
do{sta[top++]=x%10,x/=10;}while(x);
while(top) putchar(sta[--top]+'0');
putchar(t);
}
}
using namespace IO;
/*-------------head-------------*/
//
int n,m;
char a[105],b[105],t[35];
li void solve()
{
mem(a,'a');
scanf("%s%s",t,b);
per(i,m,m-n) a[i]=t[n-(m-i)];
a[m]='\0';
per(i,m,n)
a[i-n]=(b[i]-a[i]+26)%26+'a';
cout<<a<<endl;
}
int main()
{
//srand(time(0));debug();
//clock_t start_time=clock();
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//for(int QwQ=read();QwQ;QwQ--) solve();
while(~scanf("%d%d",&n,&m)) solve();
//cerr<<"Time:"<
return 0;
}