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" !!!的更多相关文章

  1. *[hackerrank]Consecutive Subsequences

    https://www.hackerrank.com/contests/w6/challenges/consecutive-subsequences 求数组中被k整除的子段和有几个.这个要利用sum[ ...

  2. ZJOI2019Round#2

    乱听课记录 关于树的分治问题&杂题选讲 张哲宇 边分治 (边分不是很鸡肋吗) 例题一 题目大意:给出两颗有正负边权的树,求出两个点\(u,v​\)使得两棵树中\((u,v)​\)距离的和最大. ...

  3. HackerRank# Wet Shark and Two Subsequences

    原题地址 对于给定的两个约束条件,可以通过联立方程组直接解出子序列A的和和子序列B的和,即sum(A) = (r + s) / 2,sum(B) = (r - s) / 2,假设|A|=|B|=n 所 ...

  4. Xtreme8.0 - Back to Square 1 数学

    Back to Square 1 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/back-to- ...

  5. Xtreme8.0 - Magic Square 水题

    Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...

  6. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  8. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  9. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

随机推荐

  1. unity htc vive使用

    本文介绍如何在Unity中使用HTC vive设备,当前VR作为市场比较火热的热点,HTC VIVE设备作为三大供应商之一,许多人购买了该设备,却不知道如何使用,本文通过图文并茂的形式,进行手把手的讲 ...

  2. 【apache】yum 安装Apache(Centos 6.5)

    一.首先在系统上面查询一下是否已经安装了apache [Apache在linux系统里的名字是httpd] rpm    -qa    httpd 如果有返回的信息,则会显示已经安装的软件.如果没有则 ...

  3. 转AjaxControlToolkit的安装与使用详解

    AjaxControlToolkit的安装与使用详解 AjaxControlToolkit下载http://ajax.asp.net/downloads/default.aspx?tabid=47ht ...

  4. PHP中字符串的连接和换行

    PHP中字符串的连接使用点,不是加号.换行\n需要用双引号括起来,不能用单引号. eg. <?php print_r("hello"."\n"); pri ...

  5. ORACLE日期格式

    to_date("要转换的字符串","转换的格式")   两个参数的格式必须匹配,否则会报错. 即按照第二个参数的格式解释第一个参数. to_char(日期,& ...

  6. WCF初探-22:WCF中使用Message类(上)

    前言 从我们学习WCF以来,就一直强调WCF是基于消息的通信机制.但是由于WCF给我们做了高级封装,以至于我们在使用WCF的时候很少了解到消息的内部机制.由于WCF的架构的可扩展性,针对一些特殊情况, ...

  7. iOS开发UI篇—Button基础

    iOS开发UI篇—Button基础 一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状 ...

  8. XML 链接

    公共Webservice   网络上可供测试的Web Service腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOn ...

  9. 内存管理单元(MMU)和协处理器CP15介绍(转)

    内存管理单元(MMU)和协处理器CP15介绍内存管理单元(MMU)介绍嵌入式系统中,存储系统差别很大,可包含多种类型的存储器件,如FLASH,SRAM,SDRAM,ROM等,这些不同类型的存储器件速度 ...

  10. Java甘特图控件swing版免费下载地址

    FlexGantt 控件是现在Java 平台下最先进的甘特图解决方案,使用一个很高的抽象层次,能适用于多种不同的域,例如 ERP 系统.生产计划和日程安排.制造流程系统或项目公文管理程序等.这些使得 ...