题目:[JSOI2007]字符加密

很常见的做法,破环为链,然后以2n为总长再后缀排序,然后对于SA[i] < n 的,说明第i小后缀的编号是小于n的,也就是说,以i开头的编号是合法的,那么输出s[SA[i]+n-1]即可。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
const int N = 2e5 + 5;
using namespace std;
int n, rk[N], sa[N], psa[N], k1[N], k2[N], cnt[N], m, len;
char s[N];
int main()
{
scanf("%s", s);
n = strlen(s);
for(int i = 0; i < n; i++)
s[i + n] = s[i];
len = n << 1;
m = max(len, 300);
for(int i = 0; i < len; i++)
cnt[(int)s[i]]++;
for(int i = 1; i < m; i++)
cnt[i] += cnt[i - 1];
for(int i = 0; i < len; i++)
rk[i] = cnt[(int)s[i]] - 1;
for(int w = 1; w < len; w<<=1)
{
for(int i = 0; i < len; i++)
{
if(i + w >= len)
k2[i] = 0;
else
k2[i] = rk[i + w];
k1[i] = rk[i];
}
for(int i = 0; i < m; i++)
cnt[i] = 0;
for(int i = 0; i < len; i++)
cnt[k2[i]]++;
for(int i = 1; i < m; i++)
cnt[i] += cnt[i - 1];
for(int i = len - 1; i >= 0; i--)
psa[--cnt[k2[i]]] = i;
for(int i = 0; i < m; i++)
cnt[i] = 0;
for(int i = 0; i < len; i++)
cnt[k1[i]]++;
for(int i = 1; i < m; i++)
cnt[i] += cnt[i - 1];
for(int i = len - 1; i >= 0; i--)
sa[--cnt[k1[psa[i]]]] = psa[i];
int pos = 1;
rk[sa[0]] = 1;
for(int i = 1; i < len; i++)
if(k1[sa[i]] == k1[sa[i - 1]] && k2[sa[i]] == k2[sa[i - 1]])
rk[sa[i]] = pos;
else
rk[sa[i]] = ++pos;
if(pos == len)
break;
}
for(int i = 0; i < len; i++)
rk[sa[i]] = i;
for(int i = 0; i < len; i++)
if(sa[i] < n)
printf("%c", s[(sa[i] + n - 1)]);
return 0;
}

最新文章

  1. asp之缓存 cachestate
  2. centos nginx server_name 配置域名访问规则
  3. Ruby:字符串处理函数
  4. springMvc全局异常处理
  5. set+几何 LA 5908 Tracking RFIDs
  6. Initialize the Storage Emulator by Using the Command-Line Tool
  7. [wordpress] determine_current_user 在get_current_user_id() 或者 wp_get_current_user()会调用
  8. BZOJ 1923 外星千足虫(高斯消元)
  9. 【07】为多态基类声明virtual析构方法
  10. 2D动态光照
  11. Windows窗体透明效果
  12. Composite Design Pattern 设计模式组合
  13. TestNg显示器(一个)-----监听器,类型和配置使用---另外META-INF详细解释
  14. 第一个小程序:helloWord
  15. 计算机网络之应用层_part -1
  16. 解决mysql、vsftp远程连接速度慢的问题
  17. Linux管理日记(二)
  18. Luogu P5290 [十二省联考2019]春节十二响
  19. Spark基础-scala学习(八、隐式转换与隐式参数)
  20. [Jenkins][git]构建时提示Caused by: hudson.plugins.git.GitException: Command &quot;/usr/bin/git reset --hard&quot; returned status code 128:

热门文章

  1. 高可用代理服务器实现keepalive+squid
  2. VSCODE 配置远程调试环境
  3. 使用kubectl管理Kubernetes(k8s)集群:常用命令,查看负载,命名空间namespace管理
  4. 给 SSH 启用二次身份验证
  5. 第2篇----Istio架构概述篇
  6. logstash 读取MySQL数据到elasticsearch 相差8小时解决办法
  7. Elasticsearch:定制分词器(analyzer)及相关性
  8. Linux下登陆MySQL时遇到报错&quot;RROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES) &quot;
  9. 【前端必会】使用indexedDB,降低环境搭建成本
  10. VMware安装Win11+WSA子系统和使用教程