Leetcode, leetcodeoj
Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE"Is a subsequence"ABCDE"While"AEC"Is not ).
Here is an example:
S ="rabbbit", T ="rabbit"
Return3.
// Dfs can be used here, but, TLE. therefore, dp is used to solve the problem. // assume that the dp [I] [j] State represents the number of methods that string I converts to string j. Then, the dynamic transfer equation is: // 1) S [I-1] = T [J-1] --> dp [I] [j] = dp [I-1] [J-1] + dp [I-1] [j] // 2) S [I-1]! = T [J-1] --> dp [I] [j] = dp [I-1] [j]. class Solution {public: int numDistinct (std: string S, std: string T) {int n = S. size (), m = T. size (); std: vector <int> dp (n + 1, std: vector <int> (m + 1, 0 )); for (int I = 0; I <n + 1; I ++) {dp [I] [0] = 1;} for (int I = 1; I <= n; I ++) {for (int j = 1; j <= m; j ++) {if (S [I-1] = T [J-1]) dp [I] [j] = dp [I-1] [J-1] + dp [I-1] [j]; else dp [I] [j] = dp [I-1] [j] ;}} return dp [n] [m] ;}};
What is leetcode?
There are many programming and interview questions, which can be compiled and run online. It is difficult. If you can do it all by yourself, it is very helpful for large companies. I just did the questions there.
How does leetcode oj submit code?
You cannot write the main function. What you need is to implement a member function based on the interface provided by class Solution.
Give a reference answer
# Include <sstream> using namespace std; class Solution {public: void reverseWords (string & s) {string ans = "", temp; stringstream sin (s ); while (sin> temp) {if (ans! = "") {Ans = temp + "" + ans;} else {ans = temp ;}} s = ans ;}};