【问题】
假设一字符串已被预处理,其中出现过的每一字符其出现的位置已被存到已排序列表(sorted list)中,例如
A = "this is a text"
预处理后会得到一下列表:
a : 9
e : 12, 17
h : 2
i : 3, 6
s : 4, 7, 13
t : 1, 11, 14, 16, 19
x : 18
空格 : 5, 8, 10, 15
对任意给定字符串B,请设计一算法找出字符串A中B出现的所有位置,算法复杂度多少?
【思想】
预处理主要是为了效率高,节省时间复杂度。稍后详解。
【代码】
/******************************************************************* * * DESCRIPTION: Pretreatment String and allocate the position * * AUTHOR: NeeSky * * DATE:2009-9-14 * *******************************************************************/ /** include files **/ #include<iostream> #include<vector>//list<int> #include<map>//STL map<char,vector<int>> using namespace std; map<char,vector<int> > charMap;//Char Dictionary /** * pretreat the string. * */ void PretreatString(const string strSample) { charMap.clear(); //Clear the Dictionary for new strGiven int len=strSample.length(); //Get the length of string for(int k=0; k<len; ++k) charMap[tolower(strSample[k])].push_back(k+1);//insert value to the map,all to be lowercase return; } /** * Output the char record table. * */ void ShowCharRecordTable(map<char,vector<int> >& tMap=charMap)/*Default Parameter*/ { if(tMap.empty())return; for(map<char,vector<int> >::iterator it=tMap.begin(); it!=tMap.end(); ++it) { cout<<it->first<<": "; vector<int>::iterator itv=it->second.begin(); cout<<(*itv++); for(;itv!=it->second.end();++itv) cout<<","<<(*itv); cout<<endl; } return; } /** * As strTemplate char order to find the first fewest char * * Just one char is enough. the position in strTemplate string. */ pair<char,int> toFindFewestCharInTable_asTemplateString(string strTemplate) { char fewestChar=strTemplate[0];int shortest=charMap[fewestChar].size();int pos=0; for(int i=1;i<strTemplate.length();++i) { if(charMap[strTemplate[i]].size()<shortest) { fewestChar=strTemplate[i]; shortest=charMap[fewestChar].size(); pos=i; } } return make_pair(fewestChar,pos); } /** * if exist, return the start position of strTemplate [right * direction first,and left direction later] else return -1 * cautious:posTemplate start from 0 * @return int */ int getOnePosition_asFewestChar(string strTemplate,int posTemplate,int posSample) { int lenTemplate=strTemplate.length();int pT=posTemplate;int pS=posSample; //Right Direction while(++pT<lenTemplate)//Not to the end { vector<int>::iterator it=find(charMap[strTemplate[pT]].begin(),charMap[strTemplate[pT]].end(),++pS); if(it==charMap[strTemplate[pT]].end())return -1; } pT=posTemplate;pS=posSample;//Reset //Left Direction while(--pT>=0)//Not over the head { vector<int>::iterator it=find(charMap[strTemplate[pT]].begin(),charMap[strTemplate[pT]].end(),--pS); if(it==charMap[strTemplate[pT]].end())return -1; } return pS; } /** * * * @author NeeSky (2009-9-20) * * @param strSample * @param strTemplate * * @return vector<int> */ vector<int> findAllOccursTemplateStr_FromSampleStr(const string strSample,const string strTemplate) { cout<<"Sample String: "<<strSample<<endl; cout<<"Template String: "<<strTemplate<<endl; vector<int> vecPos; cout<<"***Now pretreating the string:"<<strSample<<endl; PretreatString(strSample);/*Pretreat the string*/ cout<<"***Letters Table as the string:"<<endl; ShowCharRecordTable(); pair<char,int> pairCharFewest=toFindFewestCharInTable_asTemplateString(strTemplate); for(vector<int>::iterator it=charMap[pairCharFewest.first].begin();it!=charMap[pairCharFewest.first].end();++it) { int pos=getOnePosition_asFewestChar(strTemplate,pairCharFewest.second,(*it)); if(pos>=0)vecPos.push_back(pos); } return vecPos; } /** * * Just Call the Core Algorithm ,Main Program * */ int main(int argc, char* argv[]) { vector<int> vecRes=findAllOccursTemplateStr_FromSampleStr("okooook,ok okokoooooooo","ok"); cout<<"Position is: "<<endl; for(vector<int>::iterator it=vecRes.begin();it!=vecRes.end();++it) cout<<*it<<" "; cout<<endl; return 0; }
【输出】
Debug/PretreatmentStringAllocate.exe
Sample String: okooook,ok okokoooooooo
Template String: ok
***Now pretreating the string:okooook,ok okokoooooooo
***Letters Table as the string:
: 11,12
,: 8
k: 2,7,10,14,16
o: 1,3,4,5,6,9,13,15,17,18,19,20,21,22,23,24
Position is:
1 6 9 13 15
D:/WorkSpaceSlickEdit/PretreatmentStringAllocate>