163A - Substring and Subsequence - CodeForces Solution


dp *1700

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <cstring>
#define int long long
using namespace std;

const int mod = 1e9 + 7;
int sum, len1, len2, dp[5005][5005];
string a, b;

signed main()
{
	while(cin >> a >> b)
	{
		sum = 0, memset(dp, 0, sizeof dp), len1 = a.size(), len2 = b.size();
		for(int i = 0; i <= len1 - 1; i++)
			for(int j = 0; j <= len2 - 1; j++)
				dp[i + 1][j + 1] = (dp[i + 1][j] + (a[i] == b[j]) * (dp[i][j] + 1)) % mod;
		for(int i = 1; i <= len1; i++)
			sum = (sum + dp[i][len2]) % mod;
		cout << sum << endl;
	}
	return 0;
}
				 	  	  	 		 		 	     		  		


Comments

Submit
0 Comments
More Questions

1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness