回文子序列_计算回文子序列的总数
回文子序列
Problem statement:
問題陳述:
Given a string str, find total number of possible palindromic sub-sequences. A sub-sequence does not need to be consecutive, but for any xixj i<j must be valid in the parent string too. Like "icl" is a subsequence of "includehelp" while "ple" is not.
給定字符串str ,找到可能的回文子序列的總數。 子序列不必是連續的,但是對于任何x i x j i <j在父字符串中也必須有效。 像“ icl”一樣,是“ includehelp”的子序列,而“ ple”則不是。
Input:
輸入:
The first line of input contains an integer T, denoting the no of test cases then T test cases follow. Each test case contains a string str.
輸入的第一行包含一個整數T ,表示測試用例的數量,然后是T個測試用例。 每個測試用例都包含一個字符串str 。
Output:
輸出:
For each test case output will be an integer denoting the total count of palindromic subsequence which could be formed from the string str.
對于每個測試用例,輸出將是一個整數,表示回文子序列的總數,該總數可以由字符串str形成。
Constraints:
限制條件:
1 <= T <= 100 1 <= length of string str <= 300Example:
例:
Input: Test case: 2First test case: Input string: "aaaa"Output: Total count of palindromic subsequences is: 15Second test case: Input string: "abaaba"Output: Total count of palindromic subsequences is: 31Explanation:
說明:
Test case 1:
測試用例1:
Input: "aaaa"
輸入:“ aaaa”
The valid palindromic subsequences are shown below,
有效回文子序列如下所示,
Marked cells are character taken in subsequence:
標記的單元格是子序列中的字符:
Count=1
計數= 1
Count=2
計數= 2
Count=3
計數= 3
Count=4
計數= 4
Count=5
計數= 5
Count=6
計數= 6
Count=7
計數= 7
Count=8
計數= 8
Count=9
計數= 9
Count=10
數= 10
Count=11
數= 11
So on...
Total 15 palindromic sub-sequences
Actually in this case since all the character is same each and every subsequence is palindrome here.
For the second test case
Few sub-sequences can be
"a"
"b"
"a"
"aba"
So on
Total 31 such palindromic subsequences
等等...
總共15個回文子序列
實際上,在這種情況下,由于所有字符都是相同的,每個子序列在這里都是回文。
對于第二個測試用例
很少有子序列可以是
“一個”
“ b”
“一個”
“阿巴”
依此類推
總共31個這樣的回文序列
Solution approach
解決方法
This can be solved by using DP bottom up approach,
這可以通過使用DP自下而上的方法來解決,
Initialize dp[n][n] where n be the string length to 0
初始化dp [n] [n] ,其中n為0的字符串長度
Fill up the base case, Base case is that each single character is a palindrome itself. And for length of two, i.e, if adjacent characters are found to be equal then dp[i][i+1]=3, else if characters are different then dp[i][i+1]=2
填滿基本情況,基本情況是每個單個字符本身都是回文。 對于兩個長度,即,如果發現相鄰字符相等,則dp [i] [i + 1] = 3 ;否則,如果字符不同,則dp [i] [i + 1] = 2
To understand this lets think of a string like "acaa"
要理解這一點,可以考慮一個字符串,例如“ acaa”
Here
這里
dp[0][1]=2 because there's only two palindrome possible because of "a" and "c".
dp [0] [1] = 2是因為“ a”和“ c”僅可能存在兩個回文。
Whereas for
鑒于
dp[2][3] value will be 3 as possible subsequences are "a", "a", "aa".
dp [2] [3]的值將為3,因為可能的子序列為“ a”,“ a”,“ aa”。
for i=0 to n// for single length charactersdp[i][i]=1; if(i==n-1)break; if(s[i]==s[i+1])dp[i][i+1]=3;elsedp[i][i+1]=2; end forCompute for higher lengths,
計算更長的長度,
for len=3 to nfor start=0 to n-lenint end=start+len-1;// start and end is matchingif(s[end]==s[start])// 1+subsequence from semaining partdp[start][end]=1+dp[start+1][end]+dp[start][end-1];elsedp[start][end]=dp[start+1][end]+dp[start][end-1]-dp[start+1][end-1];end ifend for end forFinal result is stored in dp[0][n-1];
最終結果存儲在dp [0] [n-1]中;
So for higher lengths if starting and ending index is the same then we recur for the remaining characters, since we have the sub-problem result stored so we computed that. In case start and end index character are different then we have added dp[start+1][end] and dp[start][end-1] that's similar to recur for leaving starting index and recur for leaving end index. But it would compute dp[start+1][end-1] twice and that why we have deducted that.
因此,對于更大的長度,如果開始索引和結束索引相同,那么我們將重復其余字符,因為我們存儲了子問題結果,因此我們對其進行了計算。 如果起始索引和終止索引的字符不同,則我們添加了dp [start + 1] [end]和dp [start] [end-1] ,類似于recur離開起始索引和recur離開結束索引。 但是它將兩次計算dp [start + 1] [end-1] ,這就是為什么我們要減去它。
For proper understanding you can compute the table by hand for the string "aaaa" to understand how it's working.
為了正確理解,您可以手動計算字符串“ aaaa”的表以了解其工作方式。
C++ Implementation:
C ++實現:
#include <bits/stdc++.h> using namespace std;int countPS(string s) {int n = s.length();int dp[n][n];memset(dp, 0, sizeof(dp));for (int i = 0; i < n; i++) {dp[i][i] = 1;if (i == n - 1)break;if (s[i] == s[i + 1])dp[i][i + 1] = 3;elsedp[i][i + 1] = 2;}for (int len = 3; len <= n; len++) {for (int start = 0; start <= n - len; start++) {int end = start + len - 1;if (s[end] == s[start]) {dp[start][end] = 1 + dp[start + 1][end] + dp[start][end - 1];}else {dp[start][end] = dp[start + 1][end] + dp[start][end - 1] - dp[start + 1][end - 1];}}}return dp[0][n - 1]; }int main() {int t;cout << "Enter number of testcases\n";cin >> t;while (t--) {string str;cout << "Enter the input string\n";cin >> str;cout << "Total Number of palindromic Subsequences are: " << countPS(str) << endl;}return 0; }Output:
輸出:
Enter number of testcases 2 Enter the input string aaaa Total Number of palindromic Subsequences are: 15 Enter the input string abaaba Total Number of palindromic Subsequences are: 31翻譯自: https://www.includehelp.com/icp/count-total-number-of-palindromic-subsequences.aspx
回文子序列
總結
以上是生活随笔為你收集整理的回文子序列_计算回文子序列的总数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线程池是如何执行的?拒绝策略有哪些?
- 下一篇: 子网掩码+ip地址_C ++程序使用位掩