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 ...
随机推荐
- mysql 配置 utf8 依然乱码
mysql 乱码问题排除方案: 1.检查数据库及数据表是不是utf8字符集 2.查看一下jdbc.properties配置的数据库url 是否配置了characterEncoding=UTF-8或者在 ...
- C# Enum 简易权限设计 使用FlagsAttribute属性
基本權限設計: /// <summary> /// 權限列舉 /// </summary> [FlagsAttribute] public enum Permissions { ...
- HTC A510C电信手机刷机过程
HTC A510C电信手机刷机过程记录 Writed by Peter Hu(2014.6.7) ON WIN7_64 刷机需要的步骤: 1) 将S-ON加密保护式去掉,改成S-OFF模式,这样才能 ...
- Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法:
Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法: 1. 基本作用: Activity的 onSaveInstan ...
- terminator终端工具
terminator是个很好的终端程序,在Ubuntu Linux下安装如下: sudo apt-get install terminator 可在同一屏打开多个窗口:
- IT公司100题-28-整数的二进制表示中1的个数
问题描述: 输入一个整数n,求n的二进制表示中,一共有多少个1.例如n=8,二进制表示为00001000,二进制表示中有1个1. 分析: 如果一个数n不为0,那么n-1的二进制表示,与n的二进 ...
- 【转】request和response的页面跳转传参
下面是一位园友的文章: jsp或Servlet都会用到页面跳转,可以用 request.getRequestDispatcher("p3.jsp").forward(request ...
- enmo_day_02
Secure CRT, putty, 等终端工具 DML :u, d, i, m 增,删,改,合并 DDL : DCL : DQL : 数据字典 :存放在数据文件中,SYSTEM表空间里,纪录数据的变 ...
- 58.com qiyi
using AnfleCrawler.Common; using System; using System.Collections.Generic; using System.Linq; using ...
- iOS 开发获取唯一标识
在做流量精灵的时候有这样一个需求,帐户默认需要取手机的imsi 信息.这就有一个好处,即便用户删除我们的应用后从新下载,下次进入时他们还会以同样的身份登陆,并且获得原先纪录.这样不仅对于开发公司人员来 ...