zoukankan      html  css  js  c++  java
  • [模板]序列自动机

     仅作整理

    struct substr
    {
    	vector <int> st[256];
    
    	void init(char *x, int len)
    	{
    		for(int i = 0; i < len; ++i)
    		{
    			st[x[i]].push_back(i);
    		}
    	}
    	
    	bool isub(char *x, int len)
    	{
    		int now = -1;
    		for(int i = 0; i < len; ++i)
    		{
    			auto it = upper_bound(st[x[i]].begin(), st[x[i]] .end(), now);
    			if(it == st[x[i]].end())
    				return false;
    			else
    				now = * it;;
    		}
    		return true;
    	}
    };
    //序列自动机   
    char arr[MAXN] = {0};  
    char word[MAXN];  
    struct substr  
    {  
        struct node { int nex[26]; }pre[MAXN];  
        int cod[26];  
        void init(char *x)  
        {  
            for(int i = 0; i < 26; i++) cod[i] = -1;  
            int len = strlen(x);  
            for(int i = len - 1; i >= 0; i--)  
            {  
                for(int j = 0; j < 26; j++) pre[i].nex[j] = cod[j];  
                cod[arr[i] - 'a'] = i;  
            }  
        }  
        bool isub(char *word)  
        {  
            int len = strlen(word);  
            int nxt = -1;  
            for(int i = 0; i < len; i++)  
            {  
                int rx = word[i] - 'a';  
                if(i == 0) nxt = cod[rx];  
                else nxt = pre[nxt].nex[rx];  
                if(nxt == -1) return false;  
            }  
            return true;  
        }  
    };  
  • 相关阅读:
    HDU
    POJ
    POJ
    POJ
    POJ
    POJ
    POJ
    SPFA算法——最短路径
    POJ1251 Jungle Roads Kruskal+scanf输入小技巧
    MongoDB--关于数据库及选择MongoDB的原因
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270343.html
Copyright © 2011-2022 走看看