Algorithm about SubArrays & SubStrings

No.1 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

e.g. [−2,1,−3,4,−1,2,1,−5,4] -> [4,−1,2,1] = 6.

 1 public void maxSubSimple(int[] array) {
 2     if (array == null || array.length == 0) {
 3         System.out.println("empty array");
 4     }
 5     int max = array[0], cursum = array[0];
 6     for (int i = 1; i < array.length; i++) {
 7         cursum = cursum > 0 ? cursum + array[i] : array[i];
 8         max = max > cursum ? max : cursum;
 9     }
10     System.out.println("max sub-array sum is: " + max);
11 }
12 public void maxSubFull(int[] array) {
13     if (array == null || array.length == 0) {
14         System.out.println("empty array");
15     }
16     int max = array[0], cursum = array[0];
17     int start = 0, end = 0;
18     int final_s = 0, final_t = 0;
19     for (int i = 1; i < array.length; i++) {
20         if (cursum < 0) {
21             cursum = array[i];
22             start = i;
23             end = i;
24         } else {
25             cursum = cursum + array[i];
26             end = i;
27         }
28         if (max < cursum) {
29             max = cursum;
30             final_s = start;
31             final_t = end;
32         }
33     }
34     System.out.println("sub Array: ("+final_s+", "+final_t+")");
35     System.out.println("max sub-array sum is: " + max);
36 }

No.2 Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

e.g. [2,3,-2,4] -> [2,3] = 6.

 1 public int maxProduct(int[] A) {
 2         int premin = A[0], premax = A[0], max = A[0];
 3         for (int i = 1; i < A.length; i++) {
 4             int tmp1 = premin * A[i];
 5             int tmp2 = premax * A[i];
 6             premin = Math.min(A[i], Math.min(tmp1, tmp2));
 7             premax = Math.max(A[i], Math.max(tmp1, tmp2));
 8             max = Math.max(premax, max);
 9         }
10         return max;
11 }

No.3 LIS (Longest Increasing Subarray) 

The increasing subarray does‘t has to be contiuous nor same diff

e.g. [1, 6, 8, 3, 7, 9] -> [1, 3, 7, 9] = 4

DP, O(n^2)

 1 public void findLIS(int[] array) {
 2     if (array == null || array.length == 0) {
 3         System.out.println("Empty array");
 4         return;
 5     }
 6     int len = array.length;
 7     int[] dp = new int[len];
 8     int lis = 1;
 9     for (int i = 0; i < len; i++) {
10         dp[i] = 1;
11         for (int j = 0; j < i; j++) {
12             if (array[j] < array[i] & dp[j] + 1 > dp[i]) {
13                 dp[i] = dp[j] + 1;
14             }
15         }
16         if (dp[i] > lis) {
17             lis = dp[i];
18         }
19     }
20     System.out.println("length of Longest Increasing Subarray: "+lis);
21 }

DP + Binary Search, O(nlogn)

维护一个数组 maxval[i],记录长度为i的递增子序列中最大元素的最小值,并对于数组中的每个元素考察其是哪个子序列的最大元素,二分更新maxval数组——《编程之美》

 1 public void BinSearch(int[] maxval, int maxlen, int x) {
 2     int left = 0, right = maxlen-1;
 3     while (left <= right) {
 4         int mid = left + (right - left) / 2;
 5         if (maxval[mid] <= x) {
 6             left++;
 7         } else {
 8             right--;
 9         }
10     }
11     maxval[left] = x;
12 }
13 public void LIS(int[] array) {
14     if (array == null || array.length == 0) {
15         System.out.println("Empty array");
16         return;
17     }
18     int len = array.length;
19     int[] maxval = new int[len];
20     maxval[0] = array[0];
21     int maxlen = 1;
22     for (int i = 0; i < len; i++) {
23         if (array[i] > maxval[maxlen-1]) {
24             maxval[maxlen++] = array[i];
25         } else {
26             BinSearch(maxval, maxlen, array[i]);
27         }
28     }
29     System.out.println("length of Longest Increasing Subarray: "+maxlen);
30 }

No.4 LCS (Longest Common Subarray)

时间: 2024-10-10 14:26:37

Algorithm about SubArrays & SubStrings的相关文章

uva 10829 - L-Gap Substrings(后缀数组)

题目链接:uva 10829 - L-Gap Substrings 题目大意:给定一个字符串,问有多少字符串满足UVU的形式,要求U非空,V的长度为g. 解题思路:对字符串的正序和逆序构建后缀数组,然后枚举U的长度l,每次以长度l分区间,在l和l+d+g所在的两个区间上确定U的最大长度. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using nam

SPOJ 题目694 Distinct Substrings(后缀数组,求不同的子串个数)

DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 1000 Output For each test case output

解题报告 之 POJ1226 Substrings

解题报告 之 POJ1226 Substrings Description You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings. Input The first li

SPOJ 705 New Distinct Substrings

New Distinct Substrings Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: SUBST164-bit integer IO format: %lld      Java class name: Main Given a string, we need to find the total number of its distinct subst

CodeForces 451D Count Good Substrings

哎,最近都在做图论,没有练DP,现在一遇到DP就不会了= = 因为有合并这个操作,所以只要是首位相同的字符串肯定是能够构成good串的,那么只要统计在奇数位上出现的0,1的个数和偶数位数,随便递推一下就出来了 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #i

CodeForces 665E Beautiful Subarrays

题目:Beautiful Subarrays 链接:Here 题意:给一个数组,给一个 '完美区间' 的定义:l 到r 区间内的所有数异或和大于等于k,问给定数组有多少完美区间. 思路: 异或运算可以前缀和处理,用w[i]表示i 前面的数异或和,那么w[5]^w[3]就是4.5两数异或的值. 现在我们要开始建字典树了(感觉异或和字典树老扯上关系),我们从前往后一个个地把前缀加入字典树(补齐,高位补0),在w[i]加入字典树前,判断w[i]和前面的哪个前缀可以异或并且值大于等于k.这里要用树形DP

SPOJ694--- DISUBSTR - Distinct Substrings(后缀数组)

Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 1000 Output For each test case output one number saying the number of distin

CodeChef CSUB Count Substrings

Count Substrings Problem code: CSUB Submit All Submissions All submissions for this problem are available. Read problems statements in Mandarin Chinese and Russian. Given a string S consisting of only 1s and 0s, find the number of substrings which st

POJ 1226 Substrings

Substrings Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 122664-bit integer IO format: %lld      Java class name: Main You are given a number of case-sensitive strings of alphabetic characters, find the lar