#include <iostream>
#include <string>
#include <sstream>
#include<map>
using namespace std;
string getSearchStr(int start,int i ,string src)
{
string searchstr="";
for(int j=start;j<=i;j++)
{searchstr= searchstr.append(1,src[j]);
}
return searchstr;
}
int findStr(string src,string SearchStr,int &position1 ,int &firstMatch)
{ bool isFirstSearch=true;
int repeat=1;
while(true)
{
position1=src.find(SearchStr,position1);
if(isFirstSearch)
{
firstMatch=position1;
isFirstSearch=false;
}
int position2=src.find(SearchStr,position1+SearchStr.size());
cout<<"1:"<<position1<<endl;
cout<<"2:"<<position2<<endl;
if((position1+SearchStr.size())!=position2 || -1==position2)
{ position1=position2;
break;
}
position1=position2;
repeat++;
}
return repeat;
}
bool replaceStr(string &src,string SearchStr,int repeat,int firstMatch)
{
string temp=src;
if((repeat==2&&SearchStr.size()>=3 )||(repeat==3&&SearchStr.size()>=2 )|| (repeat>=4&&SearchStr.size()>=1) )
{
string fff;
stringstream ss;
ss<<repeat;
fff=ss.str()+"["+SearchStr+"]";
cout<<"fff:"<<fff<<endl;
string xxx=temp.replace(firstMatch,repeat*SearchStr.size(),fff);
cout<<"temp:"<<xxx<<endl;
src=temp;
return true;
}
return false;
}
int main()
{
string src;
cin>>src;
while(true)
{
bool isReplace=false;
for (int start=0;start<src.size();start++)
{
for(int i=start;i<src.size();i++)
{
int position1=0;
string SearchStr=getSearchStr(start,i,src);
//cout<<"*"<<src<<"*"<<endl;
cout<<"*"<<SearchStr<<"*"<<endl;
map <int,int> siteToReplace;
while(position1!=-1)
{
int firstMatch=-1;
int repeat=-1;
repeat=findStr(src,SearchStr,position1,firstMatch);
cout<<"**************"<<endl;
cout<<"repeat:"<<repeat<<endl;
cout<<"postion1:"<<position1<<endl;
cout<<"**************"<<endl;
siteToReplace[firstMatch]=repeat;
}
for(map<int,int>::reverse_iterator it=siteToReplace.rbegin();it!=siteToReplace.rend();++it)
{
//cout<<it->first<<"&&&&&&&&&&&&&&&&&&&"<<it->second<<endl;
isReplace=isReplace||replaceStr(src,SearchStr,it->second,it->first);
}
if(isReplace)
{
break;
}
}
if(isReplace)
{
break;
}
}
if(!isReplace)
{
break;
}
}
cout<<"$$$$$$$$$$$$$$$$$$$$$$$"<<src;
return 0;
}
别人做的!!!动态规划
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
string str;
int len[200][200];
int main()
{
cin>>str;
int i,j,k,v;
int maxlen=0,length=0;
for(i=0;i<str.size();i++)
{
len[i][i]=1;
}
for(j=1;j<str.size();j++)
{
for(i=0;i+j<str.size();i++)
{
length=j+1;
maxlen=j+1;
for(k=1;k<length;k++)
{
if((len[i][i+k-1]+len[i+k][i+length-1])<maxlen)
maxlen=(len[i][i+k-1]+len[i+k][i+length-1]);
if(length%k==0)
{
string nstr=str.substr(i,k);
int f=1;
//cout<<nstr<<" ";
for(v=1;v<length/k;v++)
{
string comstr=str.substr(i+v*k,k);
if(nstr!=comstr)
{
f=0;
break;
}
}
if(f==1)
{
if((len[i][i+k-1]+3)<maxlen)
{
maxlen=len[i][i+k-1]+3;
}
}
}
//cout<<"over"<<endl;
}
len[i][i+j]=maxlen;
}
}
cout <<len[0][str.size()-1]<< endl;
return 0;
}