ACM模板(Java)
2017-07-23 20:09
441 查看
模板
Trie
HIHOCODER1014static final int N = (int)1e5+10; static final int SIGMA=(int)27; static int ch[][]=new int[N*10][SIGMA],sz; static int var[]=new int[N*10]; static void insert(String x){ int u=0; for(int i=0;i<x.length();i++){ int c=x.charAt(i)-'a'; if(ch[u][c]==0){ for(int t=0;t<SIGMA;t++){ ch[sz][t]=0; } ch[u][c]=sz; var[sz++]=0; } u=ch[u][c]; var[u]++; } } static int search(String x){ int u=0; for(int i=0;i<x.length();i++){ int c=x.charAt(i)-'a'; if(ch[u][c]!=0) u=ch[u][c]; else return 0; } return var[u]; } static void clear() { sz=1;var[0]=0; for(int i=0;i<SIGMA;i++) { ch[0][i]=0; } }
KMP
HIHOCODER 1015写法1
static void getFail(char[] b) { int m=b.length,j=0; f[0]=f[1]=0; for(int i=1;i<m;i++) { while(j>0&&b[j]!=b[i]) j=f[j]; if(b[j]==b[i]) j++; f[i+1]=j; } } static int kmp_count(char[] a,char[] b) { int n=a.length,m=b.length; int j=0,res=0; for(int i=0;i<n;i++) { while(j>0&&b[j]!=a[i]) j=f[j]; if(b[j]==a[i]) j++; if(j==m) { j=0; res++; } } return res; }
写法2
static void getFail(char[] b,int m) { int j=0; f[1]=0; for(int i=2;i<=m;i++) { while(j>0&&b[j+1]!=b[i]) j=f[j]; if(b[j+1]==b[i]) j++; f[i]=j; } } static int kmp_count(char[] a,char[] b,int n,int m) { int j=0,res=0; for(int i=1;i<=n;i++) { while(j>0&&b[j+1]!=a[i]) j=f[j]; if(b[j+1]==a[i]) j++; if(j==m) { j=0; res++; } } return res; }
Manacher
HIHOCODER1016static final int N=(int)1e6+10; static char a[]=new char ,str[]=new char[2*N+5]; static int p[]=new int[2*N+5],len1,len2; static void Init(){ len1=a.length; str[0]='('; str[1]='#'; for(int i=0;i<len1;i++){ str[i*2+2]=a[i]; str[i*2+3]='#'; } len2=len1*2+2; str[len2]=')'; } static int Manacher(){ for(int i=0;i<len2;i++) p[i]=0; int id=0,mx=0,ans=0; for(int i=1;i<len2;i++){ if(mx>i) p[i]=Math.min(mx-i,p[2*id-i]); else p[i]=1; for(;str[i+p[i]]==str[i-p[i]];p[i]++); if(p[i]+i>mx){ mx=p[i]+i; id=i; } if(p[i]-1>ans) ans=p[i]-1; } return ans; }
Tire图
HIHOCODER 1036static int sz; static final int N=1000005; static class Node{ Node(){ post = 0; for(int i = 0; i < 26; i++) next[i] = 0; end = false; } int post; int next[]=new int[26]; boolean end; }; static Node nodes[]=new Node ; //将str添加到trie图中 static void insert(String str){ int cur=0; for(int i=0;i<str.length();i++){ if(nodes[cur].next[str.charAt(i)-'a'] == 0) nodes[cur].next[str.charAt(i)-'a'] = ++sz; cur = nodes[cur].next[str.charAt(i)-'a']; } nodes[cur].end = true; } //为trie图中的每个点添加它指向的后缀点位置 static void addPost(){ LinkedList<Integer> que = new LinkedList<Integer>(); que.push(0); int cur; while(!que.isEmpty()){ cur=que.pop(); for(int i=0;i<26;i++){ if(nodes[cur].next[i]!=0){ que.push(nodes[cur].next[i]); if(cur != 0)//不是根结点,需要设置当前点的子节点的后缀=父结点的后缀经过i到达的点 nodes[nodes[cur].next[i]].post = nodes[nodes[cur].post].next[i]; } else //nodes[current].next[i] == -1当前点经过i没有可达的 nodes[cur].next[i] = nodes[nodes[cur].post].next[i]; } } } //查找str static boolean search(String str){ int cur = 0; for(int i=0;i<str.length();i++){ if(nodes[nodes[cur].next[str.charAt(i)-'a']].end) return true; cur = nodes[cur].next[str.charAt(i)-'a']; } return false; } static void Init() { sz=0; for(int i=0;i<=1000000;i++) { nodes[i]=new Node(); } }
相关文章推荐
- ACM JAVA+C常用方法模板
- java模板 Java做acm所需要的基础知识之基本输入输出问题
- JAVA模板 Java做acm所需要的基础知识之排序问题
- Multiple of 17(湖南省第六届ACM竞赛题)含有java大数模板
- 东京大学ACM Java模板
- [置顶] Nobleman__ ACM 比赛模板 (C++ && Java)个人总结 (不断更新) (自用)
- hdu 5047 Sawtooth--2014acm上海赛区邀请赛(附java模板)
- Eclipse设置Java代码注释模板
- java 注解模板文件
- 【ACM模板】堆排序
- (Template Method)模板方法模式的Java实现 推荐
- Java的模板引擎Velocity初体验 (1)-2
- Play framework模板引擎扩展对象JavaExtensions
- Play framework模板引擎扩展对象JavaExtensions
- ACM 素数求和问题 (Java)
- Java - IntelliJ IDEA 编辑注释模板
- priority_queue sort 自定义比较终极模板 C++ & JAVA
- [ACM_数据结构] 线段树模板
- Java设计模式(四) 之 模板方法模式
- Java快捷键设置,模板设置