ural1238. Folding(记忆化)

1238

这算模拟加记忆化吗 找bug找了2个多小时。。记忆化部分好想 就是字符串处理部分挫了 一个个复制模拟 各种修改查找

  1 #include <iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<stdlib.h>
  6 #include<vector>
  7 using namespace std;
  8 #define INF 0xfffffff
  9 int dp[110][110],o[110][110];
 10 char s[110][110][110],ss[110];
 11 int len(int x)
 12 {
 13     int q=0;
 14     while(x)
 15     {
 16         q++;x/=10;
 17     }
 18     return q;
 19 }
 20 int dfs(int x,int y)
 21 {
 22     if(dp[x][y]!=INF) return dp[x][y];
 23     if(x==y)
 24     {
 25        s[x][y][0] = ss[x];
 26        s[x][y][1] = '\0';
 27        o[x][y] = 1;
 28        return dp[x][y] = 1;
 29     }
 30     int i,j,flag,k;
 31     for(i = x ; i < y ; i++)
 32     {
 33         dp[x][i] = dfs(x,i);
 34         dp[i+1][y] = dfs(i+1,y);
 35         flag = 0;
 36         k = INF;
 37         if(strcmp(s[x][i],s[i+1][y])==0)
 38         {
 39             int t = 0;
 40             if(o[x][i]+o[i+1][y]>1)
 41             t+=2;
 42             t+=len(o[x][i]+o[i+1][y]);
 43             k = strlen(s[x][i])+t;flag = 1;
 44         }
 45         if(dp[x][i]+dp[i+1][y]<k)
 46         {
 47             k = dp[x][i]+dp[i+1][y];//cout<<s[x][i]<<" "<<s[i+1][y]<<" "<<" "<<i+1<<" "<<y<<" "<<dp[x][y]<<endl;
 48         }
 49         if(dp[x][y]<=k&&!flag) continue;
 50         if(dp[x][y]>=k)
 51         {
 52             dp[x][y] = k;
 53             if(!flag)
 54             {
 55                 int g = 0,q=0;
 56                 int k1 = strlen(s[x][i]);
 57                 if(o[x][i]<=3&&k1==1)
 58                 {
 59                     for(j = 1; j <= o[x][i]; j++)
 60                     s[x][y][g++] = s[x][i][0];
 61                 }
 62                 else if(o[x][i]<=2&&k1==2)
 63                 {
 64                     for(j = 1; j <= o[x][i] ; j++)
 65                     {
 66                         s[x][y][g++] = s[x][i][0];
 67                         s[x][y][g++] = s[x][i][1];
 68                     }
 69                 }
 70                 else
 71                 {
 72                     if(o[x][i]>1)
 73                     {
 74                         int qq = o[x][i],a[10],t=0;
 75                         while(qq)
 76                         {
 77                             a[t++] = qq%10;
 78                             qq/=10;
 79                         }
 80                         for(j = t-1 ; j >= 0 ; j--)
 81                         s[x][y][g++] = a[j]+'0';
 82                         s[x][y][g++] = '(';
 83                         q = 1;
 84                     }
 85                     for(j = 0 ; j < k1;j++)
 86                     s[x][y][g++] = s[x][i][j];
 87                     if(q)
 88                     s[x][y][g++] = ')';
 89                 }
 90                 k1 = strlen(s[i+1][y]);q=0;
 91                 if(o[i+1][y]<=3&&k1==1)
 92                 {
 93                     for(j = 1; j <= o[i+1][y]; j++)
 94                     s[x][y][g++] = s[i+1][y][0];
 95                 }
 96                 else if(o[i+1][y]<=2&&k1==2)
 97                 {
 98                     for(j = 1; j <= o[i+1][y] ; j++)
 99                     {
100                         s[x][y][g++] = s[i+1][y][0];
101                         s[x][y][g++] = s[i+1][y][1];
102                     }
103                 }
104                 else
105                 {
106                     if(o[i+1][y]>1)
107                     {
108                         int qq = o[i+1][y],a[10],t=0;
109                         while(qq)
110                         {
111                             a[t++] = qq%10;
112                             qq/=10;
113                         }
114                         for(j = t-1 ; j >= 0 ; j--)
115                         s[x][y][g++] = a[j]+'0';
116                         s[x][y][g++] = '(';
117                         q = 1;
118                     }
119                     for(j = 0 ; j < k1;j++)
120                     s[x][y][g++] = s[i+1][y][j];
121                     if(q)
122                     s[x][y][g++] = ')';
123                 }
124                 o[x][y] = 1;
125                 s[x][y][g] = '\0';
126             }
127             else
128             {
129                 strcpy(s[x][y],s[x][i]);
130                 //cout<<s[x][y]<<" "<<x<<" "<<y<<endl;
131                 o[x][y] = o[x][i]+o[i+1][y];
132             }
133         }
134     }
135     return dp[x][y];
136 }
137 int main()
138 {
139     int i,j,k;
140     for(i =0  ;i <= 100 ; i++)
141     for(j =0  ;j <= 100 ; j++)
142     dp[i][j] = INF;
143     cin>>ss;
144     k = strlen(ss);
145     int minz = dfs(0,k-1),q=strlen(s[0][k-1]);
146     //cout<<q<<endl;
147     if(o[0][k-1]<=3&&q==1)
148     for(i = 1; i <= o[0][k-1] ; i++)
149     cout<<s[0][k-1][0];
150     else if(o[0][k-1]<=2&&q==2)
151     for(i = 1 ; i <= o[0][k-1] ; i++)
152     {
153         cout<<s[0][k-1][0]<<s[0][k-1][1];
154     }
155     else
156     {
157         if(o[0][k-1]>1)
158         cout<<o[0][k-1]<<'(';
159         for(i = 0 ; i < q ; i++)
160         cout<<s[0][k-1][i];
161         if(o[0][k-1]>1)
162         cout<<')';
163     }
164     puts("");
165     return 0;
166 }
View Code

 

posted @ 2014-02-11 13:32  _雨  阅读(403)  评论(0编辑  收藏  举报