回文子序列_计算回文子序列的总数
回文子序列
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 ,找到可能的回文子序列的總數(shù)。 子序列不必是連續(xù)的,但是對(duì)于任何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.
輸入的第一行包含一個(gè)整數(shù)T ,表示測(cè)試用例的數(shù)量,然后是T個(gè)測(cè)試用例。 每個(gè)測(cè)試用例都包含一個(gè)字符串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.
對(duì)于每個(gè)測(cè)試用例,輸出將是一個(gè)整數(shù),表示回文子序列的總數(shù),該總數(shù)可以由字符串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:
測(cè)試用例1:
Input: "aaaa"
輸入:“ aaaa”
The valid palindromic subsequences are shown below,
有效回文子序列如下所示,
Marked cells are character taken in subsequence:
標(biāo)記的單元格是子序列中的字符:
Count=1
計(jì)數(shù)= 1
Count=2
計(jì)數(shù)= 2
Count=3
計(jì)數(shù)= 3
Count=4
計(jì)數(shù)= 4
Count=5
計(jì)數(shù)= 5
Count=6
計(jì)數(shù)= 6
Count=7
計(jì)數(shù)= 7
Count=8
計(jì)數(shù)= 8
Count=9
計(jì)數(shù)= 9
Count=10
數(shù)= 10
Count=11
數(shù)= 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個(gè)回文子序列
實(shí)際上,在這種情況下,由于所有字符都是相同的,每個(gè)子序列在這里都是回文。
對(duì)于第二個(gè)測(cè)試用例
很少有子序列可以是
“一個(gè)”
“ b”
“一個(gè)”
“阿巴”
依此類推
總共31個(gè)這樣的回文序列
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的字符串長(zhǎng)度
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
填滿基本情況,基本情況是每個(gè)單個(gè)字符本身都是回文。 對(duì)于兩個(gè)長(zhǎng)度,即,如果發(fā)現(xiàn)相鄰字符相等,則dp [i] [i + 1] = 3 ;否則,如果字符不同,則dp [i] [i + 1] = 2
To understand this lets think of a string like "acaa"
要理解這一點(diǎn),可以考慮一個(gè)字符串,例如“ acaa”
Here
這里
dp[0][1]=2 because there's only two palindrome possible because of "a" and "c".
dp [0] [1] = 2是因?yàn)椤?a”和“ c”僅可能存在兩個(gè)回文。
Whereas for
鑒于
dp[2][3] value will be 3 as possible subsequences are "a", "a", "aa".
dp [2] [3]的值將為3,因?yàn)榭赡艿淖有蛄袨椤?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,
計(jì)算更長(zhǎng)的長(zhǎng)度,
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];
最終結(jié)果存儲(chǔ)在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.
因此,對(duì)于更大的長(zhǎng)度,如果開始索引和結(jié)束索引相同,那么我們將重復(fù)其余字符,因?yàn)槲覀兇鎯?chǔ)了子問題結(jié)果,因此我們對(duì)其進(jìn)行了計(jì)算。 如果起始索引和終止索引的字符不同,則我們添加了dp [start + 1] [end]和dp [start] [end-1] ,類似于recur離開起始索引和recur離開結(jié)束索引。 但是它將兩次計(jì)算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.
為了正確理解,您可以手動(dòng)計(jì)算字符串“ aaaa”的表以了解其工作方式。
C++ Implementation:
C ++實(shí)現(xiàn):
#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
回文子序列
總結(jié)
以上是生活随笔為你收集整理的回文子序列_计算回文子序列的总数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线程池是如何执行的?拒绝策略有哪些?
- 下一篇: 子网掩码+ip地址_C ++程序使用位掩