解法:

  对于第二个串,循环移动能得到的字典序最小的串,可以直接用最小表示法搞定。

  然后用最小表示的第二个串和第一个串做两次扩展KMP,一次正常求,另外一次将两个串都反转一下,然后扫一遍ex[]数组

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = (int)2e6+;
char a[N],b[N],c[N];
int next[N],exa[N],exb[N];
void getnext(char *s){
int len = strlen(s),cur = ;
next[] = len;
while(cur < len&&s[cur]==s[cur+])cur++;
next[] = cur;cur = ;
for(int k = ;k<len;k++){
int p = cur + next[cur] - ,L = next[k-cur];
if(k + L - >= p){
int j = (p-k+)>?(p-k+):;
while(k+j<len&&s[k+j]==s[j])j++;
next[k] = j;
cur = k;
}else
next[k] = L;
}
}
void exkmp(char *s1,char *s2,int *ex){
getnext(s2);
int l1 = strlen(s1),l2 = strlen(s2),cur = ;
while(cur < min(l1,l2)&&s1[cur]==s2[cur])cur++;
ex[] = cur;cur = ;
for(int k = ;k < l1;k++){
int p = cur + ex[cur] - ,L = next[k-cur];
if(k + L - >= p){
int j = (p-k+)>?(p-k+):;
while(k+j<l1&&j<l2&&s1[k+j]==s2[j])j++;
ex[k] = j;
cur = k;
}else
ex[k] = L;
}
}
int MinRep(char *s){
int i = ,j = ,k = ,t,len = strlen(s);
while(i<len&&j<len&&k<len){
t = s[(i+k)%len] - s[(j+k)%len];
if(t==)k++;
else{
if(t>)
i += k + ;
else
j += k + ;
if(i==j)j++;
k = ;
}
}
return min(i,j);
}
inline int dis(int now,int sz){
return min(now,sz-now-);
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
scanf("%s%s",a,b);
int start = MinRep(b);
for(int i = ,j = start;i < m;i++,j = (j+)%m)c[i] = b[j];c[m] = ;
for(int i = n;i < n+n;i++)a[i] = a[i-n];a[n+n] = ;
exkmp(a,c,exa);
reverse(a,a+*n);reverse(c,c+m);
exkmp(a,c,exb);
reverse(exb,exb+n*);
reverse(c,c+m);
reverse(a,a+*n);
int ans = -;
for(int i = ;i < n;i++)
if(exa[i] == m || exa[i] + exb[i+m-] == m-){
if(ans==-)ans = i;
else if(dis(i,n)<dis(ans,n))ans = i;
}
puts(c);
for(int i = ,j = ans;i < n;i++,j = (j+)%n)putchar(a[j]);puts("");
}
return ;
}

SGU 439 A Secret Book的更多相关文章

  1. sgu Theodore Roosevelt【判断点是否在凸多边形内模板】

    链接: http://acm.sgu.ru/problem.php?contest=0&problem=253 http://acm.hust.edu.cn/vjudge/contest/vi ...

  2. Android Secret Code

    我们很多人应该都做过这样的操作,打开拨号键盘输入*#*#4636#*#*等字符就会弹出一个界面显示手机相关的一些信息,这个功能在Android中被称为android secret code,除了这些系 ...

  3. ASP.NET OAuth:access token的加密解密,client secret与refresh token的生成

    在 ASP.NET OWIN OAuth(Microsoft.Owin.Security.OAuth)中,access token 的默认加密方法是: 1) System.Security.Crypt ...

  4. Lucky 2048 - The secret of being lucky

    Lucky 2048 uses a normal distribution to create "lucky" start. Generally speaking, it prov ...

  5. Secret Codes

    Secret Codes   This is a list of codes that can be entered into the dialer to output the listed info ...

  6. 微信企业号开发之-如何获取secret 序列号

    最近有项目基于微信企业号开发,简单记录下如何查看企业号secert 工具/原料 微信企业号   方法/步骤  用管理员的帐号登录后,选择[设置]-[权限管理]进入管理组设置界面      在左边点击[ ...

  7. SGU 495. Kids and Prizes

    水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...

  8. hdu.1111.Secret Code(dfs + 秦九韶算法)

    Secret Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. 2078 Problem H Secret Message 中石油-未提交-->已提交

    题目描述 Jack and Jill developed a special encryption method, so they can enjoy conversations without wo ...

随机推荐

  1. 本地连接批处理修改IP

    例子: 本地连接修改IP netsh interface ip delete dns "本地连接" addr=allnetsh interface ip add dns " ...

  2. cookie设置和读取以及获取超链接参数

    function setCookie(c_name, value, expiredays) { var exdate = new Date() exdate.setDate(exdate.getDat ...

  3. Android(java)学习笔记195:ContentProvider使用之添加数据到联系人(掌握)

    1.添加联系人逻辑思路 (1)首先在raw_contacts创建一个新的id (2)在data表里面添加这个id对应的数据 2.下面通过一个案例,说明一下如何添加一条数据到联系人: (1)首先我们关注 ...

  4. ALTER USER - 改变数据库用户帐号

    SYNOPSIS ALTER USER name [ [ WITH ] option [ ... ] ] where option can be: [ ENCRYPTED | UNENCRYPTED ...

  5. 实现UAC对话框效果

    设置桌面全屏: int cxScreen,cyScreen; cxScreen=GetSystemMetrics(SM_CXSCREEN); cyScreen=GetSystemMetrics(SM_ ...

  6. thinkphp5入口文件对应模块绑定

    在配置文件config.php中找到或者添加 // 入口自动绑定模块 'auto_bind_module' => true, 修改其属性为true

  7. JAVA基础——生产者消费者问题

    1.生产者消费者问题:经典案例 生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费 ...

  8. 计算机中的CPU

    今天写一下计算机中最核心的一部分,就是计算机的大脑---CPU.CPU也就是中央处理器(Central Processing Unit).中央处理器是一块超大规模的集成电路,是一台计算机的运算核心(C ...

  9. yii1框架,事务使用方法

    Yii1框架事务操作方法如下: $transaction= Yii::app()->db->beginTransaction();//创建事务 $transaction->commi ...

  10. Java权限管理(授权与认证)

    CRM权限管理 有兴趣的同学也可以阅读我最近分享的:Shiro框架原理分析   (PS : 这篇博客里面介绍了使用Shiro框架的方式实现权限管理) https://www.cnblogs.com/y ...