5555,又是DP。。。然后我又是DFS+record
其实关键是理解题意,写出DFS代码就好了吧。。。。。
map<pair<int,int>,int> record; class Solution { public: int numDistinct(string S, string T) { record.clear(); return vis(S, 0 , S.size() , T , 0 , T.size()); } private: int vis(string s , int s1 , int e1 , string t , int s2 , int e2) { if(s1 == e1) { return s2 == e2 ? 1 : 0; } if(s2 == e2) return 1; auto tmp = record.find(make_pair(s1,s2)); if(tmp != record.end()) return tmp->second; int cnt = 0; for(int i = s1 ; i < e1 ; ++i) { if(s[i] == t[s2]) { cnt += vis(s , i + 1 , e1 , t , s2 + 1 , e2); } } record.insert(make_pair(make_pair(s1,s2) , cnt)); return cnt; } };
by 1957