1. 程式人生 > >java之線段樹

java之線段樹

子串查詢

Time Limit: 3500/3000 MS (Java/Others)

Memory Limit: 262144/262144 K (Java/Others)

Problem Description

度度熊的字串課堂開始了!要以像度度熊一樣的天才為目標,努力奮鬥哦!

為了檢驗你是否具備不聽課的資質,度度熊準備了一個只包含大寫英文字母的字串 A[1,n] = a_1 a_2 \cdots a_nA[1,n]=a​1​​a​2​​⋯a​n​​,接下來他會向你提出 qq 個問題 (l,r)(l,r),你需要回答字串 A[l,r] = a_l a_{l+1} \cdots a_rA[l,r]=a​l​​a​l+1​​⋯a​r​​ 內有多少個非空子串是 A[l,r]A[l,r] 的所有非空子串中字典序最小的。這裡的非空子串是字串中由至少一個位置連續的字元組成的子序列,兩個子串是不同的當且僅當這兩個子串內容不完全相同或者出現在不同的位置。

記 |S|∣S∣ 為字串 SS 的長度,對於兩個字串 SS 和 TT ,定義 SS 的字典序比 TT 小,當且僅當存在非負整數 k(\leq \min(|S|,|T|))k(≤min(∣S∣,∣T∣)) 使得 SS 的前 kk 個字元與 TT 的前 kk 個字元對應相同,並且要麼滿足 |S| = k∣S∣=k 且 |T| > k∣T∣>k,要麼滿足 k < \min(|S|,|T|)k<min(∣S∣,∣T∣) 且 SS 的第 k+1k+1 個字元比 TT 的第 k+1k+1 個字元小。例如 "AA" 的字典序比 "AAA" 小,"AB" 的字典序比 "BA" 小。

Input

第一行包含一個整數 TT,表示有 TT 組測試資料。

接下來依次描述 TT 組測試資料。對於每組測試資料:

第一行包含兩個整數 nn 和 qq,表示字串的長度以及詢問的次數。

第二行包含一個長為 nn 的只包含大寫英文字母的字串 A[1,n]A[1,n]。

接下來 qq 行,每行包含兩個整數 l_i,r_il​i​​,r​i​​,表示第 ii 次詢問的引數。

保證 1 \leq T \leq 101≤T≤10,1 \leq n,q \leq 10^51≤n,q≤10​5​​,1 \leq l_i \leq r_i \leq n1≤l​i​​≤r​i​​≤n。

Output

對於每組測試資料,先輸出一行資訊 "Case #x:"(不含引號),其中 x 表示這是第 xx 組測試資料,接下來 qq 行,每行包含一個整數,表示字串 A[l,r]A[l,r] 中字典序最小的子串個數,行末不要有多餘空格。

Sample Input

1
2 3
AB
1 1
1 2
2 2

Sample Output

Case #1:
1
1
1

根據題目我們可以得知此題目要求我們從一段字串區間之內選出一段符合要求的“最大者”,此處最大者需要根據題目來分析。因為此處是可以採取使用線段樹來解題。

題解:我們把我們輸入的字串每一個字元存放在於線段樹的每一個節點之上,根據這一個關鍵點我們可以為每一個節點都開一個存放26個足夠標誌26個字元的陣列空間,根據線段是的特性我們由下往上推,由遞歸回去,陣列逐漸疊加,可以得知我們一段區間內哪一個字元出現的數字最多。

code:


import java.util.Scanner;

public class Main {
	 public static int record = 0;
	 public static char[] array ;
	 static Node root;

	   static class Node {
	        int left, right;
	        int []data;
	        
	        Node leftChild;
	        Node rightChild;

	        Node(int left, int right) {
	            this.left = left;
	            this.right = right;
	            this.data = new int[27];
	        }
	    }
	   
	    public void build(int left,int right){
	        root = new Node(left, right);
	        builds(root);
	    }

	    private void builds(Node root) {
	        int left = root.left;
	        int right = root.right;
	        if (right - left == 0) {
	        	root.data[(int)array[record]-64] = 1;
	        	System.out.println((int)array[record]);
	        	record++;
	            return;
	        } else  {
	            int mid = (left + right) >> 1;////將左右區間平分
	            Node leftNode = new Node(left, mid);
	            Node rightNode = new Node(mid+1, right);
	            root.leftChild = leftNode;
	            root.rightChild = rightNode;
	            builds(leftNode);
	            builds(rightNode);
	            for(int i = 1 ; i <= 26;i++) {
	            	root.data[i] = root.leftChild.data[i] + root.rightChild.data[i];
	            }
	        }
	    }
	    
	    public int[] query(int l,int r,Node root) {
	        int []temp = new int[27];
	    	if(l <= root.left&& root.right <= r ) {
	    		return root.data;
	    	}
	    	else if(root.right<l) {

	    		return temp;
	    	}
	    	else if(root.left>r) {

	    		return temp;
	    	}
	    	else {
	    		int arr[] = query(l,r,root.leftChild);
	    		int arr1[] = query(l,r,root.rightChild);
	    		for (int i = 1; i <= 26; i++) {
					temp[i] = arr[i]+arr1[i];
				}
	    		return temp;
	    	}

	    }
	    
	    public static void main(String[]args) {
	    	System.out.println(10);
			Scanner scanner = new Scanner(System.in);
			Scanner scanner1 = new Scanner(System.in);
			int n = scanner1.nextInt();
			int p = n;
			while(n!=0) {
				int a = scanner1.nextInt();
				int b = scanner1.nextInt();
				
				String nextLine = scanner.nextLine();
				array = nextLine.toCharArray();
				System.out.print("Case #"+Math.abs(p-n+1)+":\n");
				
				Main mains = new Main();
				mains.build(1, array.length);
				for(int i = 0; i < b;i++) {
					int res = scanner1.nextInt();
					int res1 = scanner1.nextInt();
					
					int tem[] = mains.query(res, res1,root);
					for (int k = 0; k < tem.length; k++) {
						if(tem[k]!=0) {
							System.out.print(tem[k]+"\n");
							break;
						}
					}
				}
				n--;
			}
			scanner.close();
			scanner1.close();
		}
}

我們需要在建立樹的時候為我們的樹賦予初值。