//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m;
map<string,int> mm;
int solve(string p){
if(p=="")
return 0;
int i,z,len,c,x,y;
string q1,q2;
if(mm.find(p)!=mm.end())
return mm[p];
c=0;
f(i,1,p.size())
if(p[i]!=p[0])
c=1;
if(!c)
return mm[p]=1;
z=p.size();
len=p.size()/2;
for(;len>=1;len--){
for(i=0;i+len<p.size();i++){
q1=p.substr(i,len);
q2=p.substr(i+len,len);
if(q1==q2){
c=1;
while(q1==p.substr(i+c*len,len)){
c++;
}
z=min(solve(p.substr(0,i))+solve(q1)+solve(p.substr(i+c*len,p.size()-i-c*len+1)),z);
}
}
}
return mm[p]=z;
}
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p;
while(cin>>p){
if(p=="*")
break;
mm.clear();
cout<<solve(p)<<endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m;
map<string,int> mm;
int solve(string p){
if(p=="")
return 0;
int i,z,len,c,x,y;
string q1,q2;
if(mm.find(p)!=mm.end())
return mm[p];
c=0;
f(i,1,p.size())
if(p[i]!=p[0])
c=1;
if(!c)
return mm[p]=1;
z=p.size();
len=p.size()/2;
for(;len>=1;len--){
for(i=0;i+len<p.size();i++){
q1=p.substr(i,len);
q2=p.substr(i+len,len);
if(q1==q2){
c=1;
while(q1==p.substr(i+c*len,len)){
c++;
}
z=min(solve(p.substr(0,i))+solve(q1)+solve(p.substr(i+c*len,p.size()-i-c*len+1)),z);
}
}
}
return mm[p]=z;
}
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p;
while(cin>>p){
if(p=="*")
break;
mm.clear();
cout<<solve(p)<<endl;
}
return 0;
}
No comments:
Post a Comment