UVa Problem 10069 Distinct Subsequences (不同的子序列)

來源:互聯網
上載者:User
// Distinct Subsequences (不同的子序列)// PC/UVa IDs: 111102/10069, Popularity: B, Success rate: average Level: 3// Verdict: Accepted// Submission Date: 2011-10-10// UVa Run Time: 0.026s//// 著作權(C)2011,邱秋。metaphysis # yeah dot net//// [Problem Description]// A subsequence of a given sequence S consists of S with zero or more elements// deleted. Formally, a sequence Z = z1 z2 . . . zk is a subsequence of X = x1// x2 . . . xm if there exists a strictly increasing sequence < i1 , i2 , . . . ,// ik > of indices of X such that for all j = 1, 2, . . . , k, we have xij = zj.// For example, Z = bcdb is a subsequence of X = abcbdab with corresponding index// sequence < 2, 3, 5, 7 >.//// Your job is to write a program that counts the number of occurrences of Z in// X as a subsequence such that each has a distinct index sequence.//// [Input]// The first line of the input contains an integer N indicating the number of test// cases to follow. The first line of each test case contains a string X, composed// entirely of lowercase alphabetic characters and having length no greater than// 10,000. The second line contains another string Z having length no greater than// 100 and also composed of only lowercase alphabetic characters. Be assured that// neither Z nor any prefix or suffix of Z will have more than 10^100 distinct// occurrences in X as a subsequence.//// [Output]// For each test case, output the number of distinct occurrences of Z in X as a// subsequence. Output for each input set must be on a separate line.//// [Sample Input]// 2// babgbag// bag// rabbbit// rabbit//// [Sample Output]// 5// 3//// [解題方法]// 此題需要使用大數運算。使用一點 DP 即可。關鍵是如何得到遞推關係,可以這樣想,設母串的長度為 j,// 子串的長度為 i,我們要求的就是長度為 i 的字串在長度為 j 的母串中出現的次數,設為 t[i][j],若// 母串的最後一個字元與子串的最後一個字元不同,則長度為 i 的子串在長度為 j 的母串中出現的次數就是// 母串的前 j - 1 個字元中子串出現的次數,即 t[i][j] = t[i][j - 1],若母串的最後一個字元與子// 串的最後一個字元相同,那麼除了前 j - 1 個字元出現字串的次數外,還要加上子串的前 i - 1 個字元// 在母串的前 j - 1 個字元中出現的次數,即 t[i][j] = t[i][j - 1] + t[i - 1][j - 1]。#include <iostream>#include <iomanip>#include <vector>#include <algorithm>using namespace std;#define MAXZ 101 class integer{friend ostream& operator<<(ostream&, const integer&);public:integer() { };// 將不帶正負號的整數轉換為大整數。integer(unsigned int orginal){do{digits.push_back(orginal % base);orginal /= base;} while (orginal);};~integer() { };integer operator+(const integer&);integer& operator+=(const integer&);private:vector < unsigned int > digits;// 數位。static unsigned int const base = 10000;// 基數。static unsigned int const width = 4;// 數位寬度。};// 重載輸出符號 <<。ostream& operator<<(ostream& os, const integer &number){os << number.digits[number.digits.size() - 1];for (int i = number.digits.size() - 2; i >= 0; i--)os << setw(number.width) << setfill('0') << number.digits[i];return os;}integer& integer::operator+=(const integer &b){return *this = *this + b;}// 加法。integer integer::operator+(const integer &b){integer c;int carry = 0;for (int i = 0; i < digits.size() || i < b.digits.size() || carry; i++){if (i < digits.size())carry += digits[i];if (i < b.digits.size())carry += b.digits[i];c.digits.push_back(carry % base);carry /= base;}return c;}void distinctSubsequences(string x, string z){// 若為二維數組,可能因資料量多而會溢出導致段錯誤,故使用滾動數組來計算節省空間的。// 當計算 t[i][j] 時,數組中儲存的是 t[i][j - 1] 和 t[i - 1][j - 1] 的資料。integer occurrences[MAXZ];int xLength = x.length();int zLength = z.length();// 為 DP 準備初始條件。長度為 0 的子串在長度為 0 的母串中出現次數為 1,長度不為 0 的// 子串在長度為 0 的母串中出現次數為 0。occurrences[0] = integer(1);for (int i = 1; i <= zLength; i++)occurrences[i] = integer(0);// DP 求出現次數。for (int i = 1; i <= xLength; i++)for (int j = zLength; j >= max(int(zLength - xLength + i), 1); j--)if (x[i - 1] == z[j - 1])occurrences[j] += occurrences[j - 1];cout << occurrences[z.length()] << endl;}int main(int ac, char *av[]){string x, z;int cases;cin >> cases;while (cases--){cin >> x >> z;if (x.length() < z.length())cout << 0 << endl;elsedistinctSubsequences(x, z);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.