UVa1630 Folding 记忆化搜索

来源:互联网 发布:centos修改ip地址 编辑:程序博客网 时间:2024/06/10 11:20

Bill is trying to compactly represent sequences of capital alphabetic characters from `A' to `Z' by folding repeating subsequences inside them. For example, one way to represent a sequence `AAAAAAAAAABABABCCD' is `10(A)2(BA)B2(C)D'. He formally defines folded sequences of characters along with the unfolding transformation for them in the following way:

  • A sequence that contains a single character from `A' to `Z' is considered to be a folded sequence. Unfolding of this sequence produces the same sequence of a single character itself.
  • If S and Q are folded sequences, thenSQ is also a folded sequence. IfS unfolds toS' andQ unfolds toQ', thenSQ unfolds toS'Q'.
  • If S is a folded sequence, then X(S) is also a folded sequence, where X is a decimal representation of an integer number greater than 1. IfS unfolds toS', thenX(S) unfolds toS' repeatedX times.

According to this definition it is easy to unfold any given folded sequence. However, Bill is much more interested in the reverse transformation. He wants to fold the given sequence in such a way that the resulting folded sequence contains the least possible number of characters.

Input 

Input file contains several test cases, one per line. Each of them contains a single line of characters from `A' to `Z' with at least 1 and at most 100 characters.

Output 

For each input case write a different output line. This must be a single line that contains the shortest possible folded sequence that unfolds to the sequence that is given in the input file. If there are many such sequences then write any one of them.

Sample Input 

AAAAAAAAAABABABCCDNEERCYESYESYESNEERCYESYESYES

Sample Output 

9(A)3(AB)CCD2(NEERC3(YES)) 

这题目的思路就是看当前的字符串是否可以折叠,如果可以就看折叠后的长度时候比之前的长度要小,用dp[i][j]表示字符串从i~j的经过折叠后的结果,如果不可以折叠,就搜寻最优的切割k使得dp[i][k].size()+dp[k+1][j].size()最小,再把结果保存下来就可以。

    #include <iostream>      #include <string>      #include <cstdio>      #include <cstring>            using namespace std;            const int maxn = 105;      string tmp;      string dp[maxn][maxn];            int isfold (int i , int j) {          int len = j-i+1;          for (int k = 1 ; k <= len/2 ; k++) {              if (len%k)                  continue;              int flag = 0;              for (int c = 0 ; c < len/k-1 ; c++) {                  for (int s = 0 ; s < k ; s++) {                      if (tmp[c*k+s+i] != tmp[c*k+k+s+i])                          flag = 1;                  }              }              if (!flag)                  return len/k;          }          return -1;      }            int main () {          while (cin >> tmp) {              int len = tmp.size();              for (int l = 1 ; l <= len ; l++) {                  for (int i = 0 ; i <= len-l ; i++) {                      int j = i+l-1;                      dp[i][j] = tmp.substr(i,j+1-i);                      int cnt = isfold(i,j);                      if (cnt > 0) {                          char num[10];                          sprintf(num,"%d",cnt);                          string tn = string(num);                          if (strlen(num)+2+(j-i+1)/cnt < j-i+1)                              dp[i][j] = tn+"("+dp[i][i+((j-i+1)/cnt)-1]+")";                      }else {                          int index = -1;                          int length = j-i+1;                          for (int k = i ; k <= j-1 ; k++)                              if (length > dp[i][k].size()+dp[k+1][j].size()) {                                  index = k;                                  length = dp[i][k].size()+dp[k+1][j].size();                              }                          if (index != -1)                              dp[i][j] = dp[i][index]+dp[index+1][j];                      }                  }              }              cout << dp[0][len-1] << endl;          }      }  









原创粉丝点击