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 ...
随机推荐
- AWS EC2的VPN-PPTP搭建教程(on aws redhat6.5 X64 centOS 6.5)
前些日子收到amazon的邮件通知,一年前申请的ec2到期了,一年免费的free tier没有了,放在上面的2个站已经欠费了十几美元了,不过我也不打算用了,准备重新注册账号(请不要鄙视我..) 1.注 ...
- mysql 配置 utf8 依然乱码
mysql 乱码问题排除方案: 1.检查数据库及数据表是不是utf8字符集 2.查看一下jdbc.properties配置的数据库url 是否配置了characterEncoding=UTF-8或者在 ...
- 如何有效使用Project(2)——进度计划的执行与监控
继上次的的<编制进度计划.保存基准>继续讲解如何对计划进行执行和监控. 计划执行即:反馈实际进度.反馈工作消耗(本文只考虑工时,不考虑成本).提出计划变更请求.如果你的企业实施了专门的PM ...
- Oracle数据库备份与恢复
第一章. 理解什么是数据库恢复 当 我们使用一个数据库时,总希望数据库的内容是可靠的.正确的,但由于计算机系统的故障(硬件故障.软件故障.网络故障.进程故障和系统故障)影响数据库系 统的操作,影响数据 ...
- Oracle 字符串分割排序冒泡算法
例子: 一个字符串"11,15,13,17,12",以逗号分割,现在要排序成"11,12,13,15,17". 写了一个实现方法,记录下来以备后用: ----- ...
- SRM 657 DIV2
-------一直想打SRM,但是感觉Topcoder用起来太麻烦了.题目还是英文,不过没什么事干还是来打一打好了.但是刚注册的号只能打DIV2,反正我这么弱也只适合DIV2了.. T1: 题目大意: ...
- Josephu--Java链表实现
public class Josephu { public static void main(String[] args) { Cyclink cyclink=new Cyclink(); cycli ...
- redis入门指南-安装redis
纸上得来终觉浅 绝知此事要躬行 Redis官方不支持window.微软发布了可在redis的分支.不建议使用 但我确实用了. win7 redis2.8.4 php5.6 apache2.4 ht ...
- Mybatis根据表自动生成相关代码
这个地方的生成需要用到三个文件,generatorConfig.xml.mybatis-generator-core-1.3.1.jar和mysql-connector-java-5.1.29.jar ...
- servlet执行流程
视频地址:http://www.imooc.com/video/5550 1-6 用户输入地址:localhost:8080/MyFirstServletDemo/index.jsp (My ...