题意:给你一个密文和明文的对应表以及一个密文+明文的字符串,明文可能只出现前面的一部分(也就是说是原明文的前缀),求最短的明文。

思路:首先密文的长度至少占到一半,所以先把那一半解密,问题转化为找一个最长的后缀使得和前缀相等,并且满足后缀长度不超过原串的一半,显然用next数组即可解决。

 #pragma comment(linker, "/STACK:10240000,10240000")

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef unsigned int uint;
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 1e3 + ;
const int md = 1e9 + ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } struct KMP {
int next[];
void GetNext(char s[]) {
mem0(next);
next[] = next[] = ;
for(int i = ; s[i]; i++) {
int j = next[i];
while(j && s[i] != s[j]) j = next[j];
next[i + ] = s[j] == s[i]? j + : ;
}
}
};
KMP kmp;
char s[], s2[], str[];
char hsh[];
int main() {
//freopen("in.txt", "r", stdin);
int T;
cin >> T;
rep_up0(cas, T) {
cin >> s;
int len = strlen(s);
rep_up0(i, len) {
hsh[s[i]] = 'a' + i;
}
cin >> s2;
len = strlen(s2);
int pos = (len + ) >> ;
for (int i = ; i < pos; i ++) s2[i] = hsh[s2[i]];
kmp.GetNext(s2);
int res = kmp.next[len];
while (len - res < pos) res = kmp.next[res];
res = len - res;
rep_up0(i, pos) str[i] = s[s2[i] - 'a'];
for (int i = pos; i < res; i ++) str[i] = s2[i];
for (int i = res; i < * res; i ++) str[i] = hsh[str[i % res]];
str[ * res] = ;
puts(str);
}
return ;
}

最新文章

  1. Neural Style学习2——环境安装
  2. 避免重定向301&amp;302 (Avoid Redirects)
  3. 【Git】标签管理
  4. C语言标量类型(转)
  5. XAML中的Path
  6. 变形虫mysql的负载均衡 读写分离
  7. [Javascript] Implement zip function
  8. GitHub 使用手册 - 基础篇
  9. Flowers(二分水过。。。)
  10. kbengine所有的demo源代码
  11. pwnable.kr login之write up
  12. React-Native 系列视频失效补链及一些碎碎念
  13. 关于delete请求,后台接收不到数据
  14. android sdk 安装 配置
  15. 通用Mapper简单使用
  16. Hadoop数据类型
  17. JAVA-大白话探索JVM-类加载器(一)
  18. 题说proxy
  19. 向kindle传送文件
  20. fiddler抓包工具使用

热门文章

  1. [Php][linux][nginx] 安装总结
  2. HTML+CSS教程(三)marquee滚动效果
  3. php获取远程文件内容的函数
  4. 任意文件下载(pikachu)
  5. [Qt] QString 常用函数
  6. MySQL事务与并发
  7. Spring5参考指南:IOC容器
  8. Android混淆配置及总结
  9. web 之 session
  10. bootstrap-分页-默认分页