本地编译结果两个: 1、//*********** 5 at touch cheat choose tact a 23 2、//*********** 3 aaa aaaa aaaaa a 19
以下是源代码:
#include<iostream>
#include<list>
#include<string>
#include<QCoreApplication>
using namespace std;
class word{
private:
list<word*> nextWord_List; //以该词的末尾一部分为开头的其他单词组成的链表
list<int> nextLen;//插入下个单词增加的长度,是下个单词的长度减去重合长度
string * str;
int count; //本单词在龙中出现的次数
public:
word():count(0){}
void countEsc(){count++;}
void countDesc(){count--;}
bool count_In_Range(){return count<2;}
string * getStr()const{return str;}
void setStr(string *str){this->str=str;}
void clearStr(){//清除字符串
delete str;
str=nullptr;
}
list<word*>* getList(){return &nextWord_List;}
list<int>* getNextLen(){return &nextLen;}
//int getLen()const{return len;}
void getAllWord_which_StartWithThisEnd(word * const allWord,int count){
for(int i=1;i<(int)str->size();i++){// i确定子串
for(int j=0;j<count;j++){// j确定其他单词
string *ortherStr=allWord[j].getStr();
string SubStr=str->substr(i);//SubStr-子串
if(
ortherStr->substr(0,SubStr.size())==SubStr //其他字符串以子串开头
&& SubStr.size()!=ortherStr->size()
)
{
//nextLen.push_back(ortherStr->size()-SubStr.size());
nextWord_List.push_back(allWord+j);
}
}
}
//检查下家和上家的最大合并,例如 aaa 和 aaaa 合并有两种方式,但最大合并是 3a+4a=6a
for(auto it=nextWord_List.begin();it!=nextWord_List.end();it++){
int len;
for(int i=1;i<(int)str->size();i++){
string SubStr=str->substr(i);
if(SubStr == (*it)->getStr()->substr(0,SubStr.size()))
len=(*it)->getStr()->size()-SubStr.size();
}
nextLen.push_back(len);
}
}
};
int MaxLength=0;
list<word*> dragon;
void creatDragon(list<word*>* newWords,list<int> *nextLen){
static int length=0;
//bengin 造龙******************************
auto it=newWords->begin();
auto itLen=nextLen->begin();
for(;it!=newWords->end();){
word *nextWord=*it;
bool FlagPush;
if(nextWord->count_In_Range()){
dragon.push_back(nextWord);
FlagPush=true;
nextWord->countEsc();
length+=(*itLen);
creatDragon(nextWord->getList(),nextWord->getNextLen());//放入新词后,继续检索新词的“次词”
}else FlagPush=false;
if(FlagPush){
dragon.pop_back();
(*it)->countDesc();
length-=(*itLen);
}
it++;
itLen++;
}
//end 造龙*********************************
//begin 比较本次龙的长度*********************
if(length>MaxLength)MaxLength=length;
};
int main(){
int N;
cin>>N;
word allWord[N];
string *str=new string[N];
for(int i=0;i<N;i++){
cin>>str[i];
allWord[i].setStr(str+i);
}
char beginChar;
cin>>beginChar;
//初始化所有单词的“下家”
for(int i=0;i<N;i++) allWord[i].getAllWord_which_StartWithThisEnd(allWord,N);
{//检测获取list<word*>的准确性
// for(int i=0;i<N;i++){
// cout<<*(allWord[i].getStr())<<"--------";
// auto it=allWord[i].getList()->begin();
// auto itInt=allWord[i].getNextLen()->begin();
// for(;it!=allWord[i].getList()->end();){
// cout<<*((*it)->getStr())<<" "<<(*itInt)<<" ";
// it++;
// itInt++;
// }
// cout<<endl;
// }
}
//list<word*> *dragon_ofMaxLength;
list<word*> BeginWord;
list<int> BeginLength;
//找到以beginChar开头的单词
for(int i=0;i<N;i++){
if(beginChar == allWord[i].getStr()->at(0))
{
BeginWord.push_back(allWord+i);
BeginLength.push_back(allWord[i].getStr()->size());
}
}
//长度知道了,下家也知道了,单词组失去意义了
for(int i=0;i<N;i++) allWord[i].clearStr();
creatDragon(&BeginWord,&BeginLength);
cout<<MaxLength<<endl;
}