uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
来源:互联网 发布:joinquant 实时数据 编辑:程序博客网 时间:2024/06/10 16:38
题目连接:10069 - Distinct Subsequences
题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z。
解题思路:二维数组DP, 有类似于求解最长公共子序列, cnt[i][j]表示在x的前j个字符中有多少个z 前i个字符。
状态转移方程
1、x[j] != z[i] cnt[i][j] = cnt[i][j - 1];
2、x[j] == z[i] cnt[i][j] = cnt[i][j - 1] + cnt[i - 1][j - 1];
计算的时候使用高精度, 并且要见j == 0的情况归1, i == 0 的情况归0。
#include <stdio.h>#include <string.h>#include <iostream>using namespace std;const int N = 10005;const int M = 105;struct bign { int len, sex; int s[M]; bign() {this -> len = 1;this -> sex = 0;memset(s, 0, sizeof(s)); } bign operator = (const char *number) {int begin = 0;len = 0;sex = 1;if (number[begin] == '-') { sex = -1; begin++;}else if (number[begin] == '+') begin++;for (int j = begin; number[j]; j++) s[len++] = number[j] - '0'; } bign operator = (int number) {char string[N];sprintf(string, "%d", number);*this = string;return *this; } bign (int number) {*this = number;} bign (const char* number) {*this = number;} bign change(bign cur) {bign now;now = cur;for (int i = 0; i < cur.len; i++) now.s[i] = cur.s[cur.len - i - 1];return now; } void delZore() {// 删除前导0.bign now = change(*this);while (now.s[now.len - 1] == 0 && now.len > 1) { now.len--;}*this = change(now); } void put() { // 输出数值。delZore();if (sex < 0 && (len != 1 || s[0] != 0)) cout << "-";for (int i = 0; i < len; i++) cout << s[i]; } bign operator + (const bign &cur){ bign sum, a, b; sum.len = 0;a = a.change(*this);b = b.change(cur);for (int i = 0, g = 0; g || i < a.len || i < b.len; i++){ int x = g; if (i < a.len) x += a.s[i]; if (i < b.len) x += b.s[i]; sum.s[sum.len++] = x % 10; g = x / 10; } return sum.change(sum); } };bign cnt[M][N], sum;char x[N], z[M];int main() { int cas; scanf("%d", &cas); while (cas--) {scanf("%s%s", x, z);int n = strlen(x), m = strlen(z);for (int i = 0; i <= n; i++) cnt[0][i] = 1;for (int i = 1; i <= m; i++) { cnt[i][0] = 0; for (int j = 1; j <= n; j++) {cnt[i][j] = cnt[i][j - 1];if (z[i - 1] == x[j - 1]) cnt[i][j] = cnt[i][j] + cnt[i - 1][j - 1]; }}cnt[m][n].put();printf("\n"); } return 0;}
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
- dp+高精度 uva-10069-Distinct Subsequences
- UVA 10069 Distinct Subsequences(dp + 高精度)
- Distinct Subsequences - UVa 10069 dp+高精度
- UVA 10069 Distinct Subsequences(dp+高精度加法)
- UVa 10069 Distinct Subsequences(大数+DP)
- UVa 10069 Distinct Subsequences(大数 DP)
- uva 10069 Distinct Subsequences (dp + 大数)
- Uva-10069 Distinct Subsequences DP
- UVA 10069 Distinct Subsequences(DP)
- UVa Problem 10069 Distinct Subsequences (不同的子序列)
- UVA - 10069 Distinct Subsequences 不同子序列
- uva 10069 - Distinct Subsequences(大数相加+DP)
- uva 10069 Distinct Subsequences(DP + 大数相加)
- uva 10069 Distinct Subsequences(dp,大数加法)
- uva 10069 Distinct Subsequences 【dp+大数】
- UVA 10069 ---Distinct Subsequences +DP+大数
- 10069 - Distinct Subsequences(高精度+动态规划)
- C++原子性实现无锁队列
- Lesson03:HTML 5 Video + DOM
- LR集合点知识
- win7 启动 mongodb
- DirectFB安装
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
- HibernateTemplate.find(hql) 在执行hql 条件语句是出错问题
- 正则表达式之“金额”匹配
- samba服务器问题日记
- c 打开文件方式(二进制和文本)
- 【摘取】敏捷测试,用户故事:一个关于“删除购物车中的某些商品”的故事评估的实例
- 字符串最大长度限制ORA-01489
- Linux进程上下文和中断上下文总结
- HAL的由来,android 为什么不是开源系统