HDU 5769 Substring(后缀数组)

题目链接:HDU 5769


题面:

Substring

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 539    Accepted Submission(s): 226


Problem Description
?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings.
But ?? thinks that is too easy, he wants to make this problem more interesting.
?? likes a character X very much, so he wants to know the number of distinct substrings which contains at least one X.
However, ?? is unable to solve it, please help him.
 

Input
The first line of the input gives the number of test cases T;T test cases follow.
Each test case is consist of 2 lines:
First line is a character X, and second line is a string S.
X is a lowercase letter, and S contains lowercase letters(‘a’-‘z’) only.

T<=30
1<=|S|<=10^5
The sum of |S| in all the test cases is no more than 700,000.
 

Output
For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the answer you get for that case.
 

Sample Input
  
  
2 a abc b bbb
 

Sample Output
  
  
Case #1: 3 Case #2: 3
Hint
In first case, all distinct substrings containing at least one a: a, ab, abc. In second case, all distinct substrings containing at least one b: b, bb, bbb.
 

Author
FZU
 

Source
 

题意:

    给定一个字符串,问该字符串中包含某一字符的不重复子串的数量。


解题:

   后缀数组的大致原理是懂的,自己写是写不出来的,勉强算是会用吧。这题可以先看一下,如何求某字符串的不重复子串的数量。

   ans=sigma(length-sa[i]-height[i]),如何理解呢,sa[i]表示的是字典序排名为i的后缀,它的起始位置,length-sa[i],即为排名i的后缀的长度,height[i],是排名为i的串和它之前那个串的公共前缀长度,故length-sa[i]-height[i],(减去和字典序前一个的公共前缀)即为不重复子串数量。

   而针对这题,需要包含特殊字符,故预先找到各个后缀中,离该后缀左侧最近的特殊字符的位置,综合该后缀不重复子串长度,取得最值。


代码:

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#define rep(i,n) for(int i = 0;i < n; i++)
#define sz 100005
#define LL long long
using namespace std;
char ori[sz];
int rk[sz],sa[sz],height[sz],w[sz],wa[sz],res[sz],pos[sz];
int min(int a,int b)
{
	return a<b?a:b;
}
int max(int a,int b)
{
	return a>b?a:b;
}
void getSa (int len,int up) 
{
	int *k = rk,*id = height,*r = res, *cnt = wa;
	rep(i,up) cnt[i] = 0;
	rep(i,len) cnt[k[i] = w[i]]++;
	rep(i,up) cnt[i+1] += cnt[i];
	for(int i = len - 1; i >= 0; i--) 
		sa[--cnt[k[i]]] = i;
	int d = 1,p = 0;
	while(p < len)
	{
		for(int i = len - d; i < len; i++) id[p++] = i;
		rep(i,len)	if(sa[i] >= d) id[p++] = sa[i] - d;
		rep(i,len) r[i] = k[id[i]];
		rep(i,up) cnt[i] = 0;
		rep(i,len) cnt[r[i]]++;
		rep(i,up) cnt[i+1] += cnt[i];
		for(int i = len - 1; i >= 0; i--)
			sa[--cnt[r[i]]] = id[i];
		swap(k,r);
		p = 0;
		k[sa[0]] = p++;
		rep(i,len-1) 
		{
			if(sa[i]+d < len && sa[i+1]+d <len &&r[sa[i]] == r[sa[i+1]]&& r[sa[i]+d] == r[sa[i+1]+d])
				k[sa[i+1]] = p - 1;
			else k[sa[i+1]] = p++;
		}
		if(p >= len) return ;
		d *= 2,up = p, p = 0;
	}
}
void getHeight(int len) 
{
	rep(i,len) rk[sa[i]] = i;
	height[0] =  0;
	for(int i = 0,p = 0; i < len - 1; i++) 
	{
		int j = sa[rk[i]-1];
		while(i+p < len&& j+p < len&& w[i+p] == w[j+p]) p++;
		height[rk[i]] = p;
		p = max(0,p - 1);
	}
}
int getSuffix(char s[]) 
{
	int len = strlen(s),up = 0;	
	for(int i = 0; i < len; i++) 
	{
		w[i] = s[i];
		up = max(up,w[i]);
	}
	w[len++] = 0;
	getSa(len,up+1);
	getHeight(len);
	return len;
}
int main()
{
    int t,l;
	char c;
	LL ans;
	scanf("%d",&t);
	for(int ix=1;ix<=t;ix++)
	{
	   ans=0;
       scanf(" %c",&c);
	   scanf("%s",ori);
       getSuffix(ori);
	   l=strlen(ori);
	   int j;
	   for(pos[l]=l,j=l-1;j>=0;j--)
		   if(ori[j]==c)
			   pos[j]=j;
	       else
			   pos[j]=pos[j+1];
	   for(int i=1;i<=l;i++)
          ans=ans+min(l-sa[i]-height[i],l-pos[sa[i]]);
	   printf("Case #%d: %lld\n",ix,ans);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,HDU1622是一道关于二叉树的题目,要求读入一系列二叉树的节点信息,输出它们的层序遍历结果。如果输入的二叉树不完整或存在重复节点,则输出"not complete"。下面是Java的实现代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static class Node { int val; Node left, right; public Node(int val) { this.val = val; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.isEmpty()) { continue; } String[] nodes = s.split("\\s+"); Node root = new Node(Integer.parseInt(nodes[0].substring(1))); Queue<Node> queue = new LinkedList<>(); queue.offer(root); boolean isComplete = true; for (int i = 1; i < nodes.length - 1; i += 2) { Node cur = queue.poll(); if (!nodes[i].equals("()")) { cur.left = new Node(Integer.parseInt(nodes[i].substring(1))); queue.offer(cur.left); } else { isComplete = false; } if (!nodes[i + 1].equals("()")) { cur.right = new Node(Integer.parseInt(nodes[i + 1].substring(0, nodes[i + 1].length() - 1))); queue.offer(cur.right); } else { isComplete = false; } } if (!isComplete) { System.out.println("not complete"); continue; } StringBuilder sb = new StringBuilder(); queue.offer(root); while (!queue.isEmpty()) { Node cur = queue.poll(); sb.append(cur.val).append(" "); if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } System.out.println(sb.toString().trim()); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值