UVA 10069 Distinct Subsequences

http://uva.onlinejudge.org/external/100/10069.pdf

两个字符串,求下一个在上一个之中出现的次数。

如果a[i] == b[j]则 dp[i][j] = dp[i-1][j] + dp[i-1][j-1]

如果a[i] != b[j]则 dp[i][j] = dp[i-1][j]

这里还涉及到高精度加法,是时候使用C语言的类了。

dp还可以转化为一维,二维的话高精度要压位,输出不能忘了补0.

一维的代码:

#include<iostream>  
#include<algorithm>
#include<math.h>
#include<cstdio>
#include<string>
#include<string.h>
using namespace std;
const int maxn = 30;
const int base = 100000000;
struct bignum
{
	int len, s[maxn];
	bignum() { memset(s, 0, sizeof(s)); len = 1; }
	bignum operator = (const bignum &b)
	{
		len = b.len;
		memcpy(s, b.s, maxn*sizeof(int));
		return *this;
	}
};

bignum operator +(const bignum &a, const bignum &b)
{
	bignum c;
	int jw = 0, i;
	for (i = 0; i < max(b.len, a.len) || jw>0; i++)
	{
		if (i < a.len) jw += a.s[i];
		if (i < b.len) jw += b.s[i];
		c.s[i] = jw % base;		jw /= base;
	}
	c.len = i;		return c;
}

int main(){
	char s[20000], a[200];
	int t;
	cin >> t;
	while (t--)
	{
		bignum f[200];
		scanf("%s%s", s, a);
		int k = strlen(a) - 1;
		f[0].s[0] = 1;
		for (int i = 0; s[i] != '\0'; i++)
			for (int j = k; j >= 0; j--)
				if (a[j] == s[i]) f[j + 1] = f[j + 1] + f[j];

		printf("%d", f[k + 1].s[f[k + 1].len - 1]);
		for (int i = f[k + 1].len - 2; i >= 0; i--) printf("%08d", f[k + 1].s[i]);
		cout << endl;
	}
	return 0;
}


你可能感兴趣的:(高精度,uva,动规)