继续分享代码。
一、工程代码及算法设计注释
--------------------------------------------------anagrams_by_stack.h----------------------------------------------
#ifndef JLU_CCST_GDC_CRASH_ANAGRAMS_BY_STACK
#define JLU_CCST_GDC_CRASH_ANAGRAMS_BY_STACK
#include <string>
extern int findStackSequences(int str1Pos,int str2Pos,std::string str1,std::string str2);
extern void testFindStackSequences();
#endif//JLU_CCST_GDC_CRASH_ANAGRAMS_BY_STACK
--------------------------------------------------anagrams_by_stack.cpp----------------------------------------------
/**
题目来源:浙大ACM在线测试——ZOJ,题目编号1004,题名"Anagrams by Stack"
URL:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004
Author:hellogdc<gdcjlu@163.com>
Finish Time:2013.12.31
*/
#include <iostream>
#include <list>
#include <stack>
#include <string>
using namespace std;
/**
算法设计:
递归思想求解:对于一个字符串而言,从其第k个字符出发求解到达目标字符串的出栈、入栈队列,
可以转化为2个子部分:
1. 将第k个字符入栈:保存字符i,然后求解从第k+1个字符出发求解到达目标字符串的出栈、入栈队列。
2. 出栈:判断出栈的字符是否与对应的目标字符串相应位置的字符相同。如相同,则说明该次出栈
成功,则保存字符o,然后继续求解从第k个字符出发求解到达目标字符串的出栈、入栈队列;否则说明
此路不通,返回。
虽然该部分与”父部分“差不多,但是不会陷入死循环,因为栈元素有限,不可能无限出栈;还有目标字符串
长度也有限,也会导致该步终会终止。
3. 递归出口:k比初始字符串长度大或者已发生字符串匹配。
注:先处理入栈,然后再处理出栈,这样保证了每个输出的栈序列都是按字典序递增的。因为入栈的是
小字符'i',出栈的是大字符'o'。
*/
/**
str1Pos:第1个字符串的当前字符位置
str2Pos:第2个字符串的当前字符位置
str1:第1个字符串
str2:第2个字符串
返回:满足条件的序列的个数
*/
static list<char> sequenceList;
static stack<char> strStack;
int findStackSequences(int str1Pos,int str2Pos,string str1,string str2){
//检查参数合法性
if(str1Pos<0||str1.length()<0||str1.length()<=0||str2.length()<=0)
return 0;
int count=0;
if(str1Pos>str1.length()||str2Pos>str2.length())
return 0;
//说明找到了一个满足条件的栈序列
if(str2Pos==str2.length()){
for(list<char>::iterator it=sequenceList.begin();it!=sequenceList.end();it++){
cout<<(*it)<<'\t';
}
cout<<endl;
return 1;
}
//入栈
//检查是否可以入栈
if(str1Pos<str1.length()){
strStack.push(str1[str1Pos]);
sequenceList.push_back('i');
count+=findStackSequences(++str1Pos,str2Pos,str1,str2);
//还原现场
strStack.pop();
sequenceList.pop_back();
str1Pos--;
}
//出栈
//检查是否可以出栈
if(!strStack.empty()){
char top=strStack.top();
if(top==str2[str2Pos]){
sequenceList.push_back('o');
strStack.pop();
count+=findStackSequences(str1Pos,++str2Pos,str1,str2);
//还原现场
strStack.push(top);
sequenceList.pop_back();
str2Pos--;
}
}
return count;
}
void testFindStackSequences(){
int count=0;
string str1="TROT";
string str2="TORT";
cout<<"*****************************************************************"<<endl;
cout<<"string1="<<str1<<endl;
cout<<"string2="<<str2<<endl;
cout<<'['<<endl;
count=findStackSequences(0,0,str1,str2);
cout<<']'<<endl;
cout<<"count="<<count<<endl;
cout<<"*****************************************************************"<<endl;
str1="madam";
str2="adamm";
cout<<"string1="<<str1<<endl;
cout<<"string2="<<str2<<endl;
cout<<'['<<endl;
count=findStackSequences(0,0,str1,str2);
cout<<']'<<endl;
cout<<"count="<<count<<endl;
cout<<"*****************************************************************"<<endl;
str1="bahama";
str2="bahama";
cout<<"string1="<<str1<<endl;
cout<<"string2="<<str2<<endl;
cout<<'['<<endl;
count=findStackSequences(0,0,str1,str2);
cout<<']'<<endl;
cout<<"count="<<count<<endl;
cout<<"*****************************************************************"<<endl;
str1="long";
str2="short";
cout<<"string1="<<str1<<endl;
cout<<"string2="<<str2<<endl;
cout<<'['<<endl;
count=findStackSequences(0,0,str1,str2);
cout<<']'<<endl;
cout<<"count="<<count<<endl;
cout<<"*****************************************************************"<<endl;
str1="eric";
str2="rice";
cout<<"string1="<<str1<<endl;
cout<<"string2="<<str2<<endl;
cout<<'['<<endl;
count=findStackSequences(0,0,str1,str2);
cout<<']'<<endl;
cout<<"count="<<count<<endl;
cout<<"*****************************************************************"<<endl;
}
--------------------------------------------------main.cpp----------------------------------------------
#if 1
#include "anagrams_by_stack.h"
int main(){
testFindStackSequences();
return 0;
}
#endif
--------------------------------------------------submit_main.cpp----------------------------------------------
/**
all the code are copyed from anagrams_by_stack.cpp
*/
#include <iostream>
#include <list>
#include <stack>
#include <string>
using namespace std;
static list<char> sequenceList;
static stack<char> strStack;
static int findStackSequences(int str1Pos,int str2Pos,string str1,string str2){
if(str1Pos<0||str1.length()<0||str1.length()<=0||str2.length()<=0)
return 0;
int count=0;
if(str1Pos>str1.length()||str2Pos>str2.length())
return 0;
if(str2Pos==str2.length()){
for(list<char>::iterator it=sequenceList.begin();it!=sequenceList.end();it++){
cout<<(*it)<<' ';
}
cout<<endl;
return 1;
}
if(str1Pos<str1.length()){
strStack.push(str1[str1Pos]);
sequenceList.push_back('i');
count+=findStackSequences(++str1Pos,str2Pos,str1,str2);
strStack.pop();
sequenceList.pop_back();
str1Pos--;
}
if(!strStack.empty()){
char top=strStack.top();
if(top==str2[str2Pos]){
sequenceList.push_back('o');
strStack.pop();
count+=findStackSequences(str1Pos,++str2Pos,str1,str2);
strStack.push(top);
sequenceList.pop_back();
str2Pos--;
}
}
return count;
}
int main(){
int count=0;
string str1="TROT";
string str2="TORT";
while(cin>>str1>>str2){
cout<<'['<<endl;
count=findStackSequences(0,0,str1,str2);
cout<<']'<<endl;
}
return 0;
}