zoukankan      html  css  js  c++  java
  • HDU4622 Reincarnation

    vjudge传送


    给一个字符串,多次询问([l,r])中不同子串的个数。((1 leqslant |S| leqslant 2000))


    因为串长才2000,所以可以考虑(O(n^2))的做法。
    (ans[l][r])表示区间([l,r])中不同子串的个数,考虑怎么递推:
    我们建立(n)个SAM,第(i)个SAM表示([S_i,S_n])。往第(i)个SAM插入位置为(j)的字符后,最后的节点为(u),那么(ans[i][j]=ans[i][j-1]+len[fa] - len[u]).
    这样做的好处在于,SAM能很容易的表示子串的尾位置(S_r),如果我们限定了(S_l),就能确定每一个子串了。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    #define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const int maxn = 2e3 + 5;
    const int maxs = 27;
    In ll read()
    {
    	ll ans = 0;
    	char ch = getchar(), las = ' ';
    	while(!isdigit(ch)) las = ch, ch = getchar();
    	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    	if(las == '-') ans = -ans;
    	return ans;
    }
    In void write(ll x)
    {
    	if(x < 0) x = -x, putchar('-');
    	if(x >= 10) write(x / 10);
    	putchar(x % 10 + '0');
    }
    
    char s[maxn];
    
    int ans[maxn][maxn];
    struct Sam
    {
    	int tra[maxn << 1][maxs], link[maxn << 1], len[maxn << 1], cnt, las;
    	In void init() 
    	{
    		link[cnt = las = 0] = -1; Mem(tra[0], 0);
    	}	
    	In void insert(int c)
    	{
    		int now = ++cnt, p = las; Mem(tra[now], 0);
    		len[now] = len[p] + 1; 
    		while(~p && !tra[p][c]) tra[p][c] = now, p = link[p];
    		if(p == -1) link[now] = 0;
    		else
    		{
    			int q = tra[p][c];
    			if(len[q] == len[p] + 1) link[now] = q;
    			else
    			{
    				int clo = ++cnt;
    				memcpy(tra[clo], tra[q], sizeof(tra[q]));
    				len[clo] = len[p] + 1;
    				link[clo] = link[q]; link[q] = link[now] = clo;
    				while(~p && tra[p][c] == q) tra[p][c] = clo, p = link[p];
    			}
    		}
    		las = now;
    	}
    }S;
    
    int main()
    {
    	int T = read();
    	while(T--)
    	{
    		Mem(ans, 0);
    		scanf("%s", s);
    		int n = strlen(s);
    		for(int i = 0; i < n; ++i)
    		{
    			S.init();
    			for(int j = i; j < n; ++j)
    			{
    				S.insert(s[j] - 'a');
    				ans[i + 1][j + 1] = ans[i + 1][j] + S.len[S.las] - S.len[S.link[S.las]];	
    			} 
    		}
    		int m = read();
    		for(int i = 1; i <= m; ++i)
    		{
    			int L = read(), R = read();
    			write(ans[L][R]), enter;
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    HDU 4864 Task(经典贪心)
    51Nod
    POJ 3122 Pie(二分+贪心)
    HDU 1053 Entropy(哈夫曼编码 贪心+优先队列)
    POJ 1328 Radar Installation(很新颖的贪心,区间贪心)
    11572
    HDU 1789 Doing Homework again(非常经典的贪心)
    合并果子(贪心+优先队列)
    CSU-ACM2018暑假集训6—BFS
    HDU 2102 A计划(两层地图加时间限制加传送门的bfs)
  • 原文地址:https://www.cnblogs.com/mrclr/p/14556955.html
Copyright © 2011-2022 走看看