回文子序列_计算回文子序列的总数
回文子序列
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 <= 300
Example:
例:
Input:
Test case: 2
First test case:
Input string:
"aaaa"
Output:
Total count of palindromic subsequences is: 15
Second test case:
Input string:
"abaaba"
Output:
Total count of palindromic subsequences is: 31
Explanation:
说明:
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 characters dp[i][i]=1; if(i==n-1) break; if(s[i]==s[i+1]) dp[i][i+1]=3; else dp[i][i+1]=2; end for
Compute for higher lengths,
计算更长的长度,
for len=3 to n for start=0 to n-len int end=start+len-1; // start and end is matching if(s[end]==s[start]) // 1+subsequence from semaining part 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]; end if end for end for
Final 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;
else
dp[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
回文子序列
回文子序列_计算回文子序列的总数相关推荐
- 1000以内的回文数_从回文诗到回文数
回文诗,顾名思义,就是能够回还往复,正读倒读皆成章句的诗篇.回文诗是我国古典诗歌中一种较为独特的体裁.明末浙江才女吴绛雪作<四时山水诗>很奇物,诗云:其实英文里也有回文.Radar 雷达R ...
- swagger导出excel文档_将Swagger2文档导出为HTML或markdown等格式离线阅读
网上有很多<使用swagger2构建API文档>的文章,该文档是一个在线文档,需要使用HTTP访问.但是在我们日常使用swagger接口文档的时候,有的时候需要接口文档离线访问,如将文档导 ...
- java api文档_细说API – 文档和前后端协作
在上一篇文章--<细说API – 重新认识RESTful>中介绍了如何理解和设计RESTful风格的API,现在我们来聊聊如何有效的呈现API文档,以及前后端协作的方式. 我经历过一些没有 ...
- python123回文素数_平方回文素数
问题描述: 素数的平方是回文,比如11*11=121. 求不超过1000的平方回文素数. 我的代码:import math def prime(m): count=0 for i in range(2 ...
- java实现最长连续子序列_最长公共子序列/最长公共子串 Python/Java实现
关注我的微信公众号:后端技术漫谈 不定期推送关于后端开发.爬虫.算法题.数据结构方面的原创技术文章,以及生活中的逸闻趣事. 我目前是一名后端开发工程师.主要关注后端开发,数据安全,网络爬虫,物联网,边 ...
- java实现最长连续子序列_最长公共子序列 ||
问题:在 前一篇文章 最长公共子序列 | 的基础上要求将所有的最长公共子序列打印出来,因为最长公共子序列可能不只一种. 难点:输出一个最长公共子序列并不难,难点在于输出所有的最长公共子序列,我们需要在 ...
- .net编写抽奖的文档_使用开源文档工具docsify,用写博客的姿势写文档
前提 ❝ 下面的简介摘抄自docsify的官网 https://docsify.js.org 中的简介 ❞ 「docsify」是一个神奇的文档网站生成器.他可以快速帮你生成文档网站.不同于GitBoo ...
- java文件头_对java文件头的解析
java对象保存在内存中时有3个部分 1.对象头 2.实例数据 3.对齐填充字节 一. 对象头 java的对象头有3部分组成: 1.Mark Word 2.指向类的指针 3.数组长度( ...
- 文件头_常见文件文件头
在日常生活中我们接触到很多软件,如QQ和微信等,这些软件都会对一些文件加密如图片加密成dat文件,这其中多数是利用文件的16进制编码进行异或运算进行加密.此处我们具体介绍一些常见的文件未进行加密前的文 ...
最新文章
- java B2B2C Springcloud多租户电子商城系统-(七)高可用的分布式配置中心(Spring Cloud Config)...
- 计算机设备采购申请,办公室采购电脑请示报告
- 《php入门很简单,PHP入门速成(1)
- spring解决ajax跨域问题
- iview template模式_使用Iview Menu 导航菜单(非 template/render 模式)
- badboy测试工具下载
- 六十九、数据结构链表的实现
- USACO Training Section 1.2 [USACO1.2]方块转换 Transformations
- 深入Atlas系列:探究序列化与反序列化能力(下) - JavaScriptSerializer
- 计算机毕业设计中java多线程与异常处理
- python读取xls文件_从python中的xls读取unicode
- Asp.net MVC 教程汇总
- cron表达式解析 3秒执行一次
- 光线追踪(RayTracing)算法理论与实践(二)平面、材质、联合光线与物体求交
- MATLAB 数学应用 微分方程 常微分方程 选择ODE求解器
- 阿里巴巴实习生初面面经
- 如何卸载“卸载驱动”图标为灰色图标
- 读取二代身份证上的相片,函数GetBmp(char * Wlt_File,int intf) 怎么用?
- Python基础-电子邮件-初识
- bm22 bm23 bm1