HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail.
I learnt the solution from this link:
https://github.com/wangyongliang/Algorithm/blob/master/hackerrank/Strings/Square%20Subsequences/main.cc
And here is his code with my comments..
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; long long lcs(string &a, string &b)
{
int sizea = a.length();
int sizeb = b.length();
vector<vector<long long>> dp(sizea, vector<long long>(sizeb)); // We only consider prefixes with a[0] matched
// to avoid duplicate counting
for(int i = ; i < sizeb; i ++)
{
if(a[] == b[i]) dp[][i] = ;
if(i) dp[][i] += dp[][i - ]; // we count accumulated
dp[][i] %= ;
} for(int i = ; i < sizea; i ++)
{
dp[i][] = dp[i - ][]; // all from dp[0][0]; part of init
for(int j = ; j < sizeb; j ++)
{
dp[i][j] = dp[i - ][j] + dp[i][j - ]; // accumulated version of LCS.
if(a[i] != b[j]) dp[i][j] -= dp[i-][j-]; // TODO: need 2nd thought on why
dp[i][j] %= ;
}
}
return dp.back().back();
} int main()
{
int t; cin >> t;
string str;
while(t--)
{
cin >> str;
long long ans = ;
for(int i = ; i < str.length(); i ++)
{
string sb = str.substr(i, str.length()- i);
string sa = str.substr(, i);
ans += lcs(sb, sa);
ans %= ;
}
cout << ans << endl;
} return ;
}
HackerRank "Square Subsequences" !!!的更多相关文章
- *[hackerrank]Consecutive Subsequences
https://www.hackerrank.com/contests/w6/challenges/consecutive-subsequences 求数组中被k整除的子段和有几个.这个要利用sum[ ...
- ZJOI2019Round#2
乱听课记录 关于树的分治问题&杂题选讲 张哲宇 边分治 (边分不是很鸡肋吗) 例题一 题目大意:给出两颗有正负边权的树,求出两个点\(u,v\)使得两棵树中\((u,v)\)距离的和最大. ...
- HackerRank# Wet Shark and Two Subsequences
原题地址 对于给定的两个约束条件,可以通过联立方程组直接解出子序列A的和和子序列B的和,即sum(A) = (r + s) / 2,sum(B) = (r - s) / 2,假设|A|=|B|=n 所 ...
- Xtreme8.0 - Back to Square 1 数学
Back to Square 1 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/back-to- ...
- Xtreme8.0 - Magic Square 水题
Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
随机推荐
- 学习JQ
目前我对jq的了解还很少,只知道jq比js简单很多,只需引入一个js文件,然后,在js中很多行才能实现的代码,jq中或许一行就搞掂了,比如根据类名获取元素,$(".类名")即可,对 ...
- Hibernate createCriteria查询详解
本文转载自 : http://penghao122.javaeye.com/blog/80794 1.创建一个Criteria实例 net.sf.hibernate.Criteria这个接口代表对一个 ...
- WCF初探-11:WCF客户端异步调用服务
前言: 在上一篇WCF初探-10:WCF客户端调用服务 中,我详细介绍了WCF客户端调用服务的方法,但是,这些操作都是同步进行的.有时我们需要长时间处理应用程序并得到返回结果,但又不想影响程序后面代码 ...
- sftp搭建
在Centos 6.6环境使用系统自带的internal-sftp搭建SFTP服务器. 打开命令终端窗口,按以下步骤操作. 0.查看openssh的版本 ssh -V 使用ssh -V 命令来查看op ...
- composer -vvv
然后在使用Composer install 或者 composer update 的时候会停住不动.使用-vvv可以输出更多信息,其命令参数输出的级别是Debug.具体可以查看composer hel ...
- Rhel6-mpich2 hpc集群配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.121 server21.example.com 192.168.12 ...
- 1-5Tomcat 目录结构 和 web项目目录结构
对应我的安装路径: web项目目录结构
- WIN7安装及配置JDK
1:什么是JDK? JDK是Java Development Kit 的简称,即Java开发工具包.JDK是ORACLE公司针对Java开发者的产品,提供了Java的开发环境和运行环境. 更多信息参看 ...
- wireshark使用方法(学习笔记一)
wireshark是非常流行的网络封包分析软件,功能十分强大.可以截取各种网络封包,显示网络封包的详细信息.使用wireshark的人必须了解网络协议,否则就看不懂wireshark了. 为了安全考虑 ...
- Linux下使用yum安装MariaDB
版本:centos7 Linux下安装MariaDB官方文档参见:https://mariadb.com/kb/zh-cn/installing-mariadb-with-yum/ 1.创建Maria ...