1. // Manacher算法,很好用。
  2.  
  3. char s[*N]; //储存临时串
  4. int save[*N];//中间记录
  5.  
  6. int Manacher(char tmp[])
  7. {
  8. int len=strlen(tmp);
  9. int cnt=;
  10. for(int i=;i<len;i++)
  11. {
  12. s[cnt++]='#';
  13. s[cnt++]=tmp[i];
  14. }
  15. s[cnt++]='#';
  16. memset(save,,sizeof(save));
  17. int a=,p=;
  18. int mx=;
  19. for(int i=;i<cnt-;i++)
  20. {
  21. if(i>=p)
  22. {
  23. int num=;
  24. while(i+num<cnt&&i-num>=&&s[i+num]==s[i-num])
  25. {
  26. num++;
  27. }
  28. p=i+num-; //能到达的最远位置
  29. a=i;
  30. save[i]=num-; //从这个位置出发最长的回文
  31. if(save[i]==) continue;
  32. if(s[i]=='#')
  33. {
  34. if(*((save[i]-)/ +)>mx) mx=*((save[i]-)/+);
  35. }
  36. else
  37. {
  38. if(*(save[i]/)+>mx) mx=*(save[i]/)+;
  39. }
  40. }
  41. else
  42. {
  43. int j=*a-i;
  44. if( i+save[j] < p)
  45. {
  46. save[i]=save[j];
  47. if(save[i]==) continue;
  48. if(s[i]=='#')
  49. {
  50. if(*((save[i]-)/ +)>mx) mx=*((save[i]-)/+);
  51. }
  52. else
  53. {
  54. if(*(save[i]/)+>mx) mx=*(save[i]/)+;
  55. }
  56. }
  57. else
  58. {
  59. int k=*i-p;
  60. while(p+<cnt&&k->=&&s[p+]==s[k-])
  61. {
  62. p++;
  63. k--;
  64. }
  65. a=i;
  66. save[i]=p-i;
  67. if(save[i]==) continue;
  68. if(s[i]=='#')
  69. {
  70. if(*((save[i]-)/ +)>mx) mx=*((save[i]-)/+);
  71. }
  72. else
  73. {
  74. if(*(save[i]/)+>mx) mx=*(save[i]/)+;
  75. }
  76. }
  77. }
  78. }
  79. return mx;
  80. }

kmp,扩展kmp

  1. // 感觉kmp可以理解。
  2.  
  3. #define N 100010
  4. int s[N];
  5. int next[N];
  6. int t[N];
  7.  
  8. int main()
  9. {
  10. //freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
  11. //freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
  12. int T;
  13. scanf("%d",&T);
  14. while(T--)
  15. {
  16. int n,m;
  17. scanf("%d%d",&n,&m);
  18. for(int i=;i<n;i++)
  19. scanf("%d",s+i);
  20. for(int i=;i<m;i++)
  21. scanf("%d",t+i);
  22. ///////////////求next
  23. int j=;
  24. next[]=;
  25. for(int i=;i<m;i++)
  26. {
  27. while(j!=&&t[j]!=t[i]) j=next[j-];
  28. if(t[j]==t[i]) j++;
  29. next[i]=j;
  30. }
  31.  
  32. /////////////////////
  33.  
  34. j=;
  35. int ans=-;
  36. for(int i=;i<n;i++)
  37. {
  38. while(j!=&&s[i]!=t[j]) j=next[j-];
  39. if(s[i]==t[j]) j++;
  40. if(j==m)
  41. {
  42. ans=i+-m+;
  43. break;
  44. }
  45. }
  46.  
  47. ///////////////////
  48. if(ans==-)
  49. printf("-1\n");
  50. else
  51. printf("%d\n",ans);
  52. }
  53. return ;
  54. }
  55.  
  56. // 扩展kmp。
  57. #defien N
  58.  
  59. int next[N],sum[N];// sum用来记录
  60.  
  61. void build(char s[])
  62. {
  63. memset(next,,sizeof(next));
  64. int p=,a=;
  65. next[]=len;
  66. for(int i=;i<len;i++)
  67. {
  68. if(i+next[i-a]-<p)
  69. {
  70. next[i]=next[i-a];
  71. }
  72. else
  73. {
  74. int k=p-i;
  75. while(p+<len && s[p+]==s[k+])
  76. {
  77. p++; k++;
  78. }
  79. p=max(p,i);
  80. next[i]=k+;
  81. a=i;
  82. }
  83. }
  84. }
  85.  
  86. void extend_kmp(char s[],char t[])//求从s到t上的最大前缀
  87. {
  88. memset(sum,,sizeof(sum));
  89. int p=-,a=;
  90. for(int i=;i<len;i++)
  91. {
  92. if(i+next[i-a]-<p)
  93. {
  94. sum[i]=next[i-a];
  95. }
  96. else
  97. {
  98. int k=p-i;
  99. while(p+<len && s[p+]==t[k+])
  100. {
  101. p++; k++;
  102. }
  103. p=max(p,i);
  104. sum[i]=k+;
  105. a=i;
  106. }
  107. }
  108. }

最小表示法

  1. int mistr(char s[],int len) //输入一串字符,返回最小表示法的起始位置
  2. {
  3. int i=,j=,k=;
  4. while(i<len&&j<len&&k<len)
  5. {
  6. if(s[i+k]==s[j+k])
  7. {
  8. k++;
  9. }
  10. else
  11. {
  12. if(s[i+k] > s[j+k]) i=i+k+;
  13. if(s[i+k] < s[j+k]) j=j+k+;
  14. k=;
  15. if(i==j) j++;
  16. }
  17. }
  18. return min(i,j);
  19. }

Manacher模板,kmp,扩展kmp,最小表示法模板的更多相关文章

  1. [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher 题解报告

    来刷kuangbin字符串了,字符串处理在ACM中是很重要的,一般比赛都会都1——2道有关字符串处理的题目,而且不会很难的那种,大多数时候都是用到一些KMP的性质或者找规律. 点击标题可跳转至VJ比赛 ...

  2. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  3. 浅谈Manacher算法与扩展KMP之间的联系

    首先,在谈到Manacher算法之前,我们先来看一个小问题:给定一个字符串S,求该字符串的最长回文子串的长度.对于该问题的求解.网上解法颇多.时间复杂度也不尽同样,这里列述几种常见的解法. 解法一   ...

  4. P5410 【模板】扩展 KMP

    P5410 [模板]扩展 KMP #include<bits/stdc++.h> using namespace std; ; int q, nxt[maxn], extend[maxn] ...

  5. 题解-洛谷P5410 【模板】扩展 KMP(Z 函数)

    题面 洛谷P5410 [模板]扩展 KMP(Z 函数) 给定两个字符串 \(a,b\),要求出两个数组:\(b\) 的 \(z\) 函数数组 \(z\).\(b\) 与 \(a\) 的每一个后缀的 L ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  7. 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法

    [KMP] 学习KMP,我们先要知道KMP是干什么的. KMP?KMPLAYER?看**? 正如AC自动机,KMP为什么要叫KMP是因为它是由三个人共同研究得到的- .- 啊跑题了. KMP就是给出一 ...

  8. ACM之路(12)—— KMP & 扩展KMP & Manacher

    最近做完了kuangbin的一套关于kmp的题目(除了一道字典树的不会,因为还没学字典树所以先放放),做个总结.(kuangbin题目的链接:http://acm.hust.edu.cn/vjudge ...

  9. Kuangbin 带你飞 KMP扩展KMP Manacher

    首先是几份模版 KMP void kmp_pre(char x[],int m,int fail[]) { int i,j; j = fail[] = -; i = ; while (i < m ...

随机推荐

  1. pandas知识点汇总

    ## pandas基础知识汇总 1.时间序列 import pandas as pd import numpy as np import matplotlib.pyplot as plt from d ...

  2. Eclipse - JAR包制作

    Eclipse - JAR包制作细节   1.Jar包分为两种,一种是不可运行的,一种是可运行的Jar包,他们的主要区别如下:     > 不可直接运行的Jar包主要是用于给别的程序提供调用   ...

  3. TCP网络传输, 数据类型的问题

    转载: http://blog.csdn.net/highfly591/article/details/45309239 1.采用TCP传输时, 应用层为什么要做超时重传: tcp保证数据可靠传输,传 ...

  4. mysql导入慢

    MySQL导出的SQL语句在导入时有可能会非常非常慢,经历过导入仅45万条记录,竟用了近3个小时.在导出时合理使用几个参数,可以大大加快导 入的速度. -e 使用包括几个VALUES列表的多行INSE ...

  5. loading数据加载的6种形式

    数据加载的几种形式及对应的交互设计 1.全屏加载 多出现在H5页面,例如微信的文章详情页.全屏加载的特点是数据一次性加载完成,内容加载完成之前界面都会停留在loading界面.进度条和有趣的动画设计, ...

  6. Windows下Python添加MySQLdb扩展模块

    [更新 2012-09-16] 这里可以下载已经打包好的EXE文件,http://sourceforge.net/projects/mysql-python/(国内需穿越才可访问) DBank备份下载 ...

  7. Atitit.antlr实现词法分析

    Atitit.antlr实现词法分析 1.1.  antlrworks-1.4.3.jar   wizard1 1.2. 词法的类型 id,int,float ,comment,str,char,wh ...

  8. [sh]清理memcached缓存

    #!/bin/bash ###author xxx ###date xxx ###清理内存缓存 used=`free -m | awk 'NR==2' | awk '{print $3}'` free ...

  9. QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout

    http://blog.csdn.net/zhuyingqingfen/article/details/6562246 如题,出现这个的原因是,如果你的窗口继承的是QMainwindow,需要设置 s ...

  10. Linux之精灵进程

    一.引言 工作中有时候可能会写一些这样的程序,它作为后台进程运行,生命周期比一般的进程要长,它在系统开机时运行,直到被强制关闭或者系统关机时退出.它就是精灵进程或者也叫做守护进程--daemon pr ...