ACM模板(Java)

模板

Trie

HIHOCODER1014
static 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

HIHOCODER1016
static final int N=(int)1e6+10;
static char a[]=new char[N],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 1036
static 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[N];
//将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();
	}
}
posted @ 2017-07-23 20:09  江南何采莲  阅读(1275)  评论(0编辑  收藏  举报