Problem:
Problem E: Optimal Rest

Music Macro Language (MML) is a language for textual representation of musical scores. Although there are various dialects of MML, all of them provide a set of commands to describe
scores, such as commands for notes, rests, octaves, volumes, and so forth.

In this problem, we focus on rests, i.e. intervals of silence. Each rest command consists of a
command specifier ‘R’ followed by a duration specifier. Each duration specifier is basically one
of the following numbers: ‘1’, ‘2’, ‘4’, ‘8’, ‘16’, ‘32’, and ‘64’, where ‘1’ denotes a whole (1), ‘2’
a half (1/2), ‘4’ a quarter (1/4), ‘8’ an eighth (1/8), and so on. This number is called the base
duration, and optionally followed by one or more dots. The first dot adds the duration by the
half of the base duration. For example, ‘4.’ denotes the duration of ‘4’ (a quarter) plus ‘8’ (an
eighth, i.e. the half of a quarter), or simply 1.5 times as long as ‘4’. In other words, ‘R4.’ is
equivalent to ‘R4R8’. In case with two or more dots, each extra dot extends the duration by the
half of the previous one. Thus ‘4..’ denotes the duration of ‘4’ plus ‘8’ plus ‘16’, ‘4...’ denotes
the duration of ‘4’ plus ‘8’ plus ‘16’ plus ‘32’, and so on. The duration extended by dots cannot
be shorter than ‘64’. For exapmle, neither ‘64.’ nor ‘16...’ will be accepted since both of the
last dots indicate the half of ‘64’ (i.e. the duration of 1/128).

In this problem, you are required to write a program that finds the shortest expressions equivalent
to given sequences of rest commands.

Input

The input consists of multiple datasets. The first line of the input contains the number of
datasets N. Then, N datasets follow, each containing a sequence of valid rest commands in one
line. You may assume that no sequence contains more than 100,000 characters.

Output

For each dataset, your program should output the shortest expression in one line. If there are
multiple expressions of the shortest length, output the lexicographically smallest one.

Sample Input

3
R2R2
R1R2R4R8R16R32R64
R1R4R16

Output for the Sample Input

R1
R1......
R16R1R4

Solution in C++ (judge verdict: Wrong Answer)

#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<29;

int NUM[28];
string SYM[28];

int ub;
pair< int,vector<string> > dfs(int a,int cur){
// printf("a=%d, ub=%d\n",a,ub);
	if(a==0){
		ub=min(ub,cur);
		return make_pair(0,vector<string>());
	}
	if(cur>=ub) return make_pair(INF,vector<string>());

	pair< int,vector<string> > S_opt(INF,vector<string>());
	rep(i,28) if(NUM[i]<=a) {
		pair< int,vector<string> > S=dfs(a-NUM[i],cur+SYM[i].length());
		S.first+=SYM[i].length();
		S.second.push_back(SYM[i]);
		if(S.first<S_opt.first){
			S_opt=S;
		}
// printf("%d\n",i_opt);
	}
	return S_opt;
}

int main(){
	int n=0;
	SYM[n]="R1......"; NUM[n++]=64+32+16+8+4+2+1;
	SYM[n]="R1.....";  NUM[n++]=64+32+16+8+4+2;
	SYM[n]="R1....";   NUM[n++]=64+32+16+8+4;
	SYM[n]="R1...";    NUM[n++]=64+32+16+8;
	SYM[n]="R1..";     NUM[n++]=64+32+16;
	SYM[n]="R1.";      NUM[n++]=64+32;
	SYM[n]="R16..";    NUM[n++]=4+2+1;
	SYM[n]="R16.";     NUM[n++]=4+2;
	SYM[n]="R16";      NUM[n++]=4;
	SYM[n]="R1";       NUM[n++]=64;
	SYM[n]="R2.....";  NUM[n++]=32+16+8+4+2+1;
	SYM[n]="R2....";   NUM[n++]=32+16+8+4+2;
	SYM[n]="R2...";    NUM[n++]=32+16+8+4;
	SYM[n]="R2..";     NUM[n++]=32+16+8;
	SYM[n]="R2.";      NUM[n++]=32+16;
	SYM[n]="R2";       NUM[n++]=32;
	SYM[n]="R32.";     NUM[n++]=2+1;
	SYM[n]="R32";      NUM[n++]=2;
	SYM[n]="R4....";   NUM[n++]=16+8+4+2+1;
	SYM[n]="R4...";    NUM[n++]=16+8+4+2;
	SYM[n]="R4..";     NUM[n++]=16+8+4;
	SYM[n]="R4.";      NUM[n++]=16+8;
	SYM[n]="R4";       NUM[n++]=16;
	SYM[n]="R64";      NUM[n++]=1;
	SYM[n]="R8...";    NUM[n++]=8+4+2+1;
	SYM[n]="R8..";     NUM[n++]=8+4+2;
	SYM[n]="R8.";      NUM[n++]=8+4;
	SYM[n]="R8";       NUM[n++]=8;

	int T; scanf("%d",&T);
	while(T--){
		int sum=0;
		for(getchar();;){
			int a,c; scanf("%d",&a);
			sum+=64/a;
			while(c=getchar(),c=='.') sum+=64/(a*=2);
			if(c!='R') break;
		}

		int one=max(sum/64,0)/3*3;
		sum-=64*one;

// printf("sum=%d, one=%d\n",sum,one,0);
		ub=INF;
		vector<string> S=dfs(sum,0).second;
// printf("ub=%d, sz=%d\n",ub,S.size());
		reverse(S.begin(),S.end());

		bool b=true;
		rep(i,S.size()){
			if(b && one>0 && "R1."<S[i]){
				rep(j,one/3) printf("R1.R1.");
				b=false;
			}
			printf("%s",S[i].c_str());
		}
		if(b && one>0){
			rep(j,one/3) printf("R1.R1.");
			b=false;
		}
		putchar('\n');
	}

	return 0;
}