class Solution {
public:
int numDistinct(string s, string t) {
if(t.size()<=0) return 1;
if((s.size()<=0 && t.size()>0) || s.size()<t.size()){
return 0;
}
int ssize = s.size();
int tsize = t.size();
vector<vector <int> > matchs(tsize ,vector<int>(ssize,0));
int tcursor = tsize - 1;
int sStart = tcursor;
int sEnd = tcursor - tsize + ssize;
int scursor = sEnd;
while(tcursor>=0){
sStart = tcursor;
sEnd = tcursor - tsize + ssize;
scursor = sEnd;
while(scursor>=sStart){
//cout<<"111 "<<matchs[tcursor][scursor]<<" "<< tcursor<<" "<<scursor<< " "<<sStart<<" "<<ssize<<endl;
if(t[tcursor] == s[scursor]){
if(scursor>=ssize - 1 && tcursor>=tsize - 1)
matchs[tcursor][scursor] += 1;
else if(tcursor>=tsize - 1){
matchs[tcursor][scursor] += 1+matchs[tcursor][scursor+1];
}
else{
matchs[tcursor][scursor] = matchs[tcursor+1][scursor+1] + matchs[tcursor][scursor+1];
//cout<<matchs[tcursor][scursor]<<" "<< tcursor<<" "<<scursor<< " "<<sStart<<" "<<ssize<<endl;
}
}else{
if(scursor>=ssize - 1 && tcursor>=tsize - 1)
matchs[tcursor][scursor] =0;
else
matchs[tcursor][scursor] = matchs[tcursor][scursor+1];
}
scursor--;
}
tcursor--;
}
return matchs[0][0];
}
public:
int numDistinct(string s, string t) {
if(t.size()<=0) return 1;
if((s.size()<=0 && t.size()>0) || s.size()<t.size()){
return 0;
}
int ssize = s.size();
int tsize = t.size();
vector<vector <int> > matchs(tsize ,vector<int>(ssize,0));
int tcursor = tsize - 1;
int sStart = tcursor;
int sEnd = tcursor - tsize + ssize;
int scursor = sEnd;
while(tcursor>=0){
sStart = tcursor;
sEnd = tcursor - tsize + ssize;
scursor = sEnd;
while(scursor>=sStart){
//cout<<"111 "<<matchs[tcursor][scursor]<<" "<< tcursor<<" "<<scursor<< " "<<sStart<<" "<<ssize<<endl;
if(t[tcursor] == s[scursor]){
if(scursor>=ssize - 1 && tcursor>=tsize - 1)
matchs[tcursor][scursor] += 1;
else if(tcursor>=tsize - 1){
matchs[tcursor][scursor] += 1+matchs[tcursor][scursor+1];
}
else{
matchs[tcursor][scursor] = matchs[tcursor+1][scursor+1] + matchs[tcursor][scursor+1];
//cout<<matchs[tcursor][scursor]<<" "<< tcursor<<" "<<scursor<< " "<<sStart<<" "<<ssize<<endl;
}
}else{
if(scursor>=ssize - 1 && tcursor>=tsize - 1)
matchs[tcursor][scursor] =0;
else
matchs[tcursor][scursor] = matchs[tcursor][scursor+1];
}
scursor--;
}
tcursor--;
}
return matchs[0][0];
}