<Leetcode>Distinct Subsequences
来源:互联网 发布:淘宝进店自动打招呼 编辑:程序博客网 时间:2024/06/11 12:46
Given a string
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"
"ABCDE"
"AEC"
Here is an example:
S "rabbbit"
, "rabbit"
Return 3
.
链接:http://oj.leetcode.com/problems/distinct-subsequences/
第一遍直接用的傻逼搜索,给TLE跪,只能继续想办法。
想降低时间复杂度,于是用了很复杂的数据结构,先进行预处理,对于模板串的每个字母Ct,计算出它所能代表的字串长度,比如abba中,a的字串长度可能有2种:1和4,b的长度可能有2种:2和3。然后对于源串S,从后向前对每个字母进行初始化,每个S中的字符都有一个桶结构去维护其各种字串长度信息,各子串信息存在结点中,记录累加值acc和位置(模板串T中的)pos值(pos和子串长度一一对应,记录pos即可)。预处理复杂度为O(S.length)。
#include<iostream>#include<string>#include<memory>#include<assert.h>using namespace std;#define S_MAX_LEN1000#define T_MAX_LEN50class Node{public:int pos;int acc;};class NodeBucket{public:int len;Node *node[32];NodeBucket():len(0){}NodeBucket &operator=(const NodeBucket &m){len = m.len;for(int i=0;i<len;i++)this->node[i] = m.node[i];return *this;}};class Solution{public:string DropTheSource(const string &s,const string &t){bool v[256];string sRet;char buf[S_MAX_LEN];int len=0;memset(v,0,sizeof(v));for(int i=0;i<sLen;i++)v[s[i]]=true;for(int i=0;i<tLen;i++)if(!v[t[i]])return sRet;memset(v,0,sizeof(v));for(int i=0;i<tLen;i++)v[t[i]]=true;for(int i=0;i<sLen;i++)if(v[s[i]])buf[len++] = s[i];buf[len] = '\0';sRet = buf;return sRet;}void init1(const string &s,const string &t){NodeBucket* m[256];memset(m,0,sizeof(m));//scan the template str for making NodeBucket by character.for(int i=0;i<tLen;i++){if(m[t[i]]==NULL){m[t[i]] = &nodeBucketPool[nbpLen++];m[t[i]]->len=0;}Node* ptmp = &nodepool[npLen++];ptmp->acc=0;ptmp->pos = i;m[t[i]]->node[ m[t[i]]->len++ ] = ptmp;}//scan the source str and append the NodeBucketfor(int i=0;i<sLen;i++){nodeBucketPool[nbpLen] = *m[s[i]];nodeBucket[i] = &nodeBucketPool[nbpLen++];}return;}inline int Get_Acc_of_Sub_Str_In_Temp(int Last_Pos[],const string &t,int len){//get the acc of the char of substr with length len in the SourceStrif(len<1)return 0;int pos = Last_Pos[ t[tLen-len] ];if(pos==-1)return 0;int pos1 = tLen - len;for(int i=0;i<nodeBucket[pos]->len;i++)if(nodeBucket[pos]->node[i]->pos == pos1)return nodeBucket[pos]->node[i]->acc;assert(false);return -1;}int Dp(const string &s,const string &t){int T_last_pos[256],length,tmp1,tmp2;memset(T_last_pos,-1,sizeof(T_last_pos));for(int i=sLen-1;i>=0;i--){for(int j=0;j<nodeBucket[i]->len;j++){length = tLen - nodeBucket[i]->node[j]->pos;//the length of the substrtmp1 = Get_Acc_of_Sub_Str_In_Temp(T_last_pos,t,length);if(length==1)nodeBucket[i]->node[j]->acc = tmp1+1;else{tmp2 = Get_Acc_of_Sub_Str_In_Temp(T_last_pos,t,length-1);nodeBucket[i]->node[j]->acc = tmp1 + tmp2;}}T_last_pos[s[i]] = i;}return Get_Acc_of_Sub_Str_In_Temp(T_last_pos,t,tLen);}int numDistinct(string S, string T){int ret;npLen = nbpLen = 0;sLen = S.length(); tLen = T.length();//1.Drop the character from the source string which is excluded by the template string.string s = DropTheSource(S,T);sLen = s.length();if(sLen==0)return 0;//2.Load the nodeBucket array.init1(s,T);//3.Calculate the nodeBucket by DP.ret = Dp(s,T);return ret;}int sLen,tLen;Node nodepool[5*T_MAX_LEN];int npLen;NodeBucket nodeBucketPool[S_MAX_LEN + T_MAX_LEN];int nbpLen;NodeBucket *nodeBucket[S_MAX_LEN];};int main(){string a,b;while(cin>>a>>b){Solution s;cout<<s.numDistinct(a,b)<<endl;}return 0;}
- LeetCode Distinct Subsequences
- [LeetCode]Distinct Subsequences
- [Leetcode] Distinct Subsequences *
- [leetcode] Distinct Subsequences
- [LeetCode] Distinct Subsequences
- [Leetcode] Distinct Subsequences
- leetcode 111: Distinct Subsequences
- LeetCode Distinct Subsequences
- [leetcode] Distinct Subsequences
- LeetCode - Distinct Subsequences
- Leetcode:Distinct Subsequences
- [LeetCode]Distinct Subsequences
- [LeetCode] Distinct Subsequences
- [Leetcode]Distinct Subsequences
- [leetcode]Distinct Subsequences
- Leetcode: Distinct Subsequences
- Leetcode: Distinct Subsequences
- [leetcode] Distinct Subsequences
- cocos2d-x(屏幕旋转,schedule,系统语言)
- PHP "十二五" 简易 BLOG 代码- 未测试- 09
- 使用Editplus编译运行Java程序
- 我的编程语录一:砌砖的过程
- 关于数据库SQL
- <Leetcode>Distinct Subsequences
- PHP "十二五" 简易 BLOG 代码- 未测试- 10
- 新晋技术经理的管理难题
- 数据结构之 非递归中序遍历二叉树
- Hello & Goodbye
- php下载文件代码实现
- 新建了个博客网站
- UVa 11384 Help is needed for Dexter / 二分
- Display, add and delete data from an NSTableView Implement NSTableView.