Connection Status:
Competition Arena > CountingCommonSubsequences
SRM 298 · 2006-04-11 · by soul-net · Dynamic Programming, Recursion
Class Name: CountingCommonSubsequences
Return Type: long
Method Name: countCommonSubsequences
Arg Types: (string, string, string)
Problem Statement

Problem Statement

A subsequence of a string is obtained by removing zero or more characters from it. You are given three Strings, and must determine the number of different non-empty subsequences they all share.

Notes

  • A subsequence should be counted only once even if it can be obtained in more than 1 way from any of the input Strings.

Constraints

  • a, b and c will have between 1 and 50 characters each, inclusive.
  • Every character of a, b and c will be a lowercase letter ('a'-'z').
Examples
0)
"call"
"accelerate"
"candle"
Returns: 6

The 6 subsequences common to all of the strings are: "c", "a", "l", "al", "ca" and "cl".

1)
"topcoder"
"topcoder"
"topcoder"
Returns: 239

All possible subsequences are shared by all three strings. Note that the letter 'o' is repeated, so some of the subsequences may be formed in multiple ways. Each unique subsequence is only counted once.

2)
"no"
"correct"
"answer"
Returns: 0

There are many subsequences that are shared by any two of these strings, but none that are shared by all three of them.

3)
"aabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabb"
"abababababababababababababababababababab"
"aaaabbbbaaaabbbbaaaabbbbaaaabbbbaaaabbbb"
Returns: 1725660
4)
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Returns: 50

Submissions are judged against all 115 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class CountingCommonSubsequences with a public method long long countCommonSubsequences(string a, string b, string c) · 115 test cases · 2 s / 256 MB per case

Submitting as anonymous