3 solutions

  • 0
    @ 2026-8-4 10:27:26

    [乐谱] 标程深度学习:编译期状态机折叠与 DP 回溯

    基础信息:省选/NOI-(紫题) | 编译原理预处理、状态正交分离、背包 DP 【隔离警告:严禁输出模式二的考点分析和策略!】

    1. 思路来源与取舍

    普通的做法总是试图通过局部替换(比如 L4R4 -> L2R2)来压缩乐谱,但这在带环境状态变量(如全局 L 和 O)的情况下,极易陷入局部最优的死胡同。 原作者看透了这种状态机的本质:不要做局部替换,直接把它当成一门编程语言来编译! 作者写了一个前端解析器(Parser),把所有的相对状态修改(如 <,>,L8)全部执行掉,解析出每一刻的“绝对音符”和“绝对时长的连续休止符”。由于所有的 OO 和 LL 状态被“拍平”到了每个事件上,原问题被完美降维成了:给定绝对事件序列,求解用最少字符重构它的路径。

    2. 核心代码掰开揉碎讲

    • 解耦解析器 (Parser):

      if(s[i]=='>') ++O;
      else if(s[i]=='<') --O;
      else if(t[0]=='V') V=stoll(t.substr(1));
      else if(t[0]=='R') a.pb(large{1,{},x});
      else a.pb(large{0,tonote(t,O,L),{}});
      

      这是整个算法的基石。作者用极其工整的模拟,将原字符串转变成了 large(即事件 Event)的动态数组 a。特别地,对于所有的 V,由于它绝对独立(不影响音长和八度),作者直接把它绑定在后一个音符的 Vl[x] 数组中,彻底踢出了 DP 状态,这是绝佳的剪枝。

    • 休止符的最短表示预处理 (Rest DP):

      int t = 1 + j + (i != s1)*lll[i] + (s1 != p)*(1+lll[p]) + dp[p][s2][len-Len[i][j]];
      

      休止符的拼凑相当于一个“从起点默认 L=s1 走到终点默认 L=s2,总代价为 len”的完全背包问题。 作者在 init() 中直接预处理了 0∼25600 \sim 2560 长度(对应 20 个全音符)的最小花费。 降维大招:对于超过 25602560 的天量休止符怎么办?

      int b = a % 1280 + 1280;
      return (a - b) / 128 + comb[x][y][b];
      

      利用周期的贪心思想!12801280 正好是 1010 个全音符。如果长度超过它,最划算的做法绝对是切换到 L1,然后无脑写 R(每个 R 代表 1 个全音符,消耗 1 字符)。所以只需剥离出多余的 128 整数倍,剩下的尾数交给预处理即可。这避免了无限大小的 DP 表!

    • 主旋律自动机 DP:

      g[i][j2][k2] = min(..., f[i][j][k] + calcO(k,k2) + calcL(j,j2));
      

      f 数组是事件结算后的状态,g 数组是为了迎合下一个事件,而做出的 O 和 L 切换代价。这种把“环境切换 (g)”和“事件本身执行 (f)”拆分成两层递推的写法,逻辑极度清晰,避免了在同一层 for 循环中枚举前后状态带来的思维灾难。

    3. 代码架构与底层细节

    • 音符的绝对同余映射 (mp 初始化): 钢琴上有黑键和白键,题目中说 C+ 等价于 D-。作者没有写复杂的 if-else,而是直接建了一个哈希表:mp["C+"] = mp["D-"] = 1。这样后续只需要比对哈希值,即可完美解决同音异名的重组问题!
    • 避免 MLE 的回溯生成:
      void dfs(int x,int s1,int s2) { ... dfs(x, si, sj); ... cout << "C-"; }
      
      在 DP 推演中,f 数组里存的仅仅是 long long 的最小代价值(即 cost),绝对没有存 std::string。当找到终点的最小值后,利用 dfs 沿着最小代价的等式倒推,顺藤摸瓜在递归回溯时直接 cout。这种“仅在最终路径上输出”的架构,使得内存占用维持在 MB 级别,常数极小!

    4. 工程还原与调试推演

    在手搓这段代码时,最容易死在休止符的长度计算上: int t=128/L; a.l=__lg(t); 题目输入可能是 L4,也可能是直接接在 R 后面的 R8。作者把所有的长度统一到了 128 为基本单位。比如 L4 就是 128/4=32128 / 4 = 32 单位;附点 . 的实现是 L += cur /= 2,严格使用整型位运算,断绝了一切浮点数精度爆炸的可能。

    5. 适用场景与死穴

    • 适用场景:带状态寄存器的指令流压缩 / 最短等价代码生成。凡是遇到编译器后端的“指令选择”、“死代码消除”类题型,先用解释器拍平为绝对指令流,再做正交状态机的多阶段决策 DP!
    • 死穴:特判缺失。作者特意加了 if(a[i].a.w1<8 && a[i].a.w1>=0) 和 if(k2>=1 && a[i].a.w2==0) 等条件。这是因为题目有一条非常阴险的规定:“在八度 1 不播放 C-,在八度 8 不播放 B+”。如果在重构代码时不避开这个规则,生成的极简代码会导致播放效果异常,喜提 WA。

    6. 带详尽注释的完整代码

    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
     */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         998244353
    #define speMOD      2933256077ll
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<endl;return;}
    
    // [NOTE] 全局基础工具函数
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    
    map<string,int>mp;
    int Len[200][200];
    int comb[8][8][2600];
    // 记录 L1, L2, L4... 变为字符串时需要的额外长度
    int lll[8]={3,2,2,2,1,1,1,1}; 
    int llen[12]={1,2,1,2,1,1,2,1,2,1,2,1};
    // 12 平均律名称
    string S[12]={"C","D-","D","E-","E","F","F+","G","A-","A","B-","B"};
    
    struct note{
        // w1: 绝对八度 (0~7 对应 O1~O8)
        // w2: 音名哈希 (0~11)
        // l: 长度种类 (0~7 对应 L128 ~ L1)
        // len: 附点数量
    	int w1,w2;int l,len;
    	void out(int x){
    		cout<<S[w2];
    		if(x!=l)cout<<128/(1<<l); // 显式声明音长
    		REP(i,0,len)cout<<'.';
    	}
    };
    
    note tonote(string s,int id=4,int L=4){
    	--id;
    	// 归一化极端的越界音符表达
    	if(s.size()>=2&&s[0]=='B'&&s[1]=='+'){ ++id; s="C"+s.substr(2,s.size()-2); }
    	else if(s.size()>=2&&s[0]=='C'&&s[1]=='-'){ --id; s="B"+s.substr(2,s.size()-2); }
    	
    	int len=1;
    	if(s.size()>1&&(s[1]=='+'||s[1]=='-')) len=2;
    	note a;
    	a.w1=id; a.w2=mp[s.substr(0,len)];
    	s=s.substr(len,s.size()-len);
    	int t=0;
    	if(!s.size()||!isdigit(s[0])){
    		a.l=__lg(128/L);a.len=s.size();
    	}else{
    		int cnt=0;
    		REP(i,0,s.size())if(isdigit(s[i]))t=t*10+s[i]-48; else ++cnt;
    		t=128/t; a.l=__lg(t); a.len=cnt;
    	}
    	return a;
    }
    
    int dp[8][8][2600];
    tuple<int,int,int>from[8][8][2600];
    
    void output(int x,int y,int s){
    	if(!s){
    		if(x!=y)cout<<'L'<<128/(1<<y);
    		return;
    	}
    	auto [i,j,k]=from[x][y][s];
    	cout<<'R';
    	if(i!=x)cout<<128/(1<<i);
    	REP(l,0,j)cout<<'.';
    	if(k!=x)cout<<'L'<<128/(1<<k);
    	output(k,y,s-Len[i][j]);
    }
    
    struct Rest{
    	int a;
    	void init(string s,int L=4){
    		s=s.substr(1,s.size()-1);
    		if(s.size()&&isdigit(s[0])){
    			int lst=0;L=0;
    			REP(i,0,s.size())if(isdigit(s[i]))++lst,L=L*10+s[i]-48;
    			s=s.substr(lst,s.size()-lst);
    		}
    		L=128/L;
    		int cur=L;
    		REP(i,0,s.size()) L+=cur/=2; // 完美整数解决附点时长
    		a=L;
    	}
    	// [大背包周期优化]
    	int calc(int x,int y){
    		if(a<=2560)return dp[x][y][a];
    		int b=a%1280+1280;
    		return (a-b)/128+comb[x][y][b];
    	}
    	void out(int x,int y){
    		if(a<=2560){ output(x,y,a); }
    		else{
    			int b=a%1280+1280;
    			int t1=-1,t2=-1,t3=-1;
    			REP(i,0,2561)if(i<=b){
    				REP(j,0,8){
    					REP(k,0,8){
    						if(comb[x][y][b]==dp[x][j][i]+(j!=7)*2+(k!=7)*(1+lll[k])+dp[k][y][b-i]){
    							t1=j;t2=k;t3=i;break;
    						}
    					}
    					if(t1!=-1)break;
    				}
    				if(t1!=-1)break;
    			}
    			output(x,t1,t3); if(t1!=7)cout<<"L1";
    			REP(i,0,(a-b)/128)cout<<'R';
    			if(t2!=7)cout<<'L'<<128/(1<<t2);
    			output(t2,y,b-t3);
    		}
    	}
    };
    
    struct large{ int op; note a; Rest b; };
    
    int mn1[2600][8],mn2[2600][8];
    
    void init() {
    	// 建立 12 平均律同音异名的 Hash 表
    	mp["B+"]=mp["C"]=0;mp["C+"]=mp["D-"]=1;
    	mp["D"]=2;mp["D+"]=mp["E-"]=3;
    	mp["E"]=mp["F-"]=4;mp["F"]=mp["E+"]=5;
    	mp["F+"]=mp["G-"]=6;mp["G"]=7;
    	mp["G+"]=mp["A-"]=8;mp["A"]=9;
    	mp["A+"]=mp["B-"]=10;mp["B"]=mp["C-"]=11;
    	
    	// 预处理不同附点下的时长 Len[i][j]
    	REP(i,0,8){
    		int cur=(1<<i);
    		REP(j,0,i+1){
    			cur|=(1<<(i-j));
    			Len[i][j]=cur;
    		}
    	}
    	
    	// 预处理 DP Rest 背包
    	REP(i,0,8){ REP(j,0,8) dp[i][j][0]=i==j? 0:1+lll[j]; }
    	REP(len,1,2561){
    		REP(s1,0,8){
    			REP(s2,0,8){
    				dp[s1][s2][len]=1e9;
    				REP(i,0,8){
    					REP(j,0,i+1)if(Len[i][j]<=len){
    						REP(p,0,8){
    							int t=1+j+(i!=s1)*lll[i]+(s1!=p)*(1+lll[p])+dp[p][s2][len-Len[i][j]];
    							if(t<=dp[s1][s2][len]) dp[s1][s2][len]=t, from[s1][s2][len]={i,j,p};
    						}
    					}
    				}
    			}
    		}
    	}
    	
    	// 预计算合并状态
    	REP(i,0,2561){
    		REP(s1,0,8){
    			int mn=1e9;
    			REP(s2,0,8)mn=min(mn,dp[s1][s2][i]+(s2!=7)*2);
    			mn1[i][s1]=mn;
    			mn=1e9;
    			REP(s2,0,8)mn=min(mn,dp[s2][s1][i]+(s2!=7)*(1+lll[s2]));
    			mn2[i][s1]=mn;
    		}
    	}
    	REP(i,0,2561){ REP(j,0,8){ REP(k,0,8) comb[j][k][i]=1e9; } }
    	REP(i,0,2561){
    		REP(s1,0,8){
    			REP(j,0,2561)if(i+j<=2560){
    				REP(s2,0,8) comb[s1][s2][i+j]=min(comb[s1][s2][i+j],mn1[i][s1]+mn2[j][s2]);
    			}
    		}
    	}
    }
    
    int calcO(int x,int y){return x==y? 0:min(2,abs(x-y));} // 相差 1 步用 <>,2 步及以上直接切 O
    int calcL(int x,int y){return x==y? 0:1+lll[y];}
    
    int f[100005][8][8],g[100005][8][8];
    vector<large>a;
    int ps1[100005],ps2[100005];
    int Vl[100005];
    int curV;
    
    // 回溯生成绝对最佳字符串(防 MLE,仅生成 1 次)
    void dfs(int x,int s1,int s2){
    	ps1[x-1]=s1;ps2[x-1]=s2;
    	int si=0,sj=0;
    	REP(i,0,8){
    		REP(j,0,8){
    			if(g[x][s1][s2]==f[x][i][j]+calcO(j,s2)+calcL(i,s1)) si=i,sj=j;
    		}
    	}
    	if(x){
    		--x;
    		if(a[x].op){ // 是休止符
    			int J=-1;
    			REP(j,0,8)if(g[x][j][sj]+a[x].b.calc(j,si)==f[x+1][si][sj]){J=j;break;}
    			dfs(x,J,sj); a[x].b.out(J,si);
    		}else{ // 是音符
    			if(a[x].a.w1==sj){
    				dfs(x,si,sj);
    				if(Vl[x]!=curV) cout<<'V'<<(curV=Vl[x]); // 独立判定音量
    				a[x].a.out(si);
    			}else if(a[x].a.w1-1==sj){ // 高八度代换 (避免跨八度)
    				dfs(x,si,sj);
    				if(Vl[x]!=curV)cout<<'V'<<(curV=Vl[x]);
    				cout<<"B+";
    				if(a[x].a.l!=si)cout<<128/(1<<a[x].a.l);
    				REP(i,0,a[x].a.len)cout<<'.';
    			}else{ // 低八度代换
    				dfs(x,si,sj);
    				if(Vl[x]!=curV)cout<<'V'<<(curV=Vl[x]);
    				cout<<"C-";
    				if(a[x].a.l!=si)cout<<128/(1<<a[x].a.l);
    				REP(i,0,a[x].a.len)cout<<'.';
    			}
    		}
    	}
    	if(si!=s1) cout<<'L'<<(128/(1<<s1));
    	if(sj!=s2){
    		if(sj+1==s2) cout<<'>';
    		else if(sj-1==s2) cout<<'<';
    		else cout<<'O'<<s2+1;
    	}
    }
    
    void Main(){
    	string s;
    	cin>>s;
    	int L=4,O=4,V=100;
    	a.clear();
    	
    	// 编译器前端解析:拍平所有环境状态
    	REP(i,0,s.size()){
    		if(s[i]=='>') ++O;
    		else if(s[i]=='<') --O;
    		else if(isalpha(s[i])){
    			int j=i+1;
    			while(j<(int)s.size()&&(!isalpha(s[j])&&s[j]!='<'&&s[j]!='>'))++j;
    			--j;
    			string t=s.substr(i,j-i+1);
    			if(t[0]=='O') O=t[1]-48;
    			else if(t[0]=='L'){ t=t.substr(1,t.size()-1); L=stoll(t); }
    			else if(t[0]=='V'){ t=t.substr(1,t.size()-1); V=stoll(t); }
    			else if(t[0]=='R'){
    				Rest x; x.init(t,L);
    				if(a.size()&&a.back().op){ a.back().b.a+=x.a; } // 合并相邻的休止块
    				else a.pb(large{1,{},x});
    			}else{
    				a.pb(large{0,tonote(t,O,L),{}});
    				Vl[a.size()-1]=V; // 音量剥离
    			}
    			i=j;
    		}
    	}
    	
    	// 主 DP
    	curV=100;
    	REP(i,0,8) REP(j,0,8) f[0][i][j]=1e9;
    	f[0][5][3]=0; // 起始 O4(idx 3), L4(idx 5)
    	
    	int n=a.size();
    	REP(i,0,n){
    		REP(j,0,8) REP(k,0,8) f[i+1][j][k]=g[i][j][k]=1e9;
    		
    		// 切换成本
    		REP(j,0,8) REP(k,0,8) REP(j2,0,8) REP(k2,0,8)
    		    g[i][j2][k2]=min(g[i][j2][k2],f[i][j][k]+calcO(k,k2)+calcL(j,j2));
    		
    		if(a[i].op){
    			REP(j,0,8) REP(j2,0,8) REP(k,0,8)
    			    f[i+1][j2][k]=min(f[i+1][j2][k],g[i][j][k]+a[i].b.calc(j,j2));
    		}else{
    			int k2=a[i].a.w1;
    			if(a[i].a.w1<8&&a[i].a.w1>=0){
    				REP(j,0,8) f[i+1][j][k2]=min(f[i+1][j][k2],g[i][j][k2]+a[i].a.len+(a[i].a.l!=j)*lll[a[i].a.l]+llen[a[i].a.w2]);
    			}
    			// 防踩雷死穴:O1 的 C- 禁用,只能从 B+ 借;O8 的 B+ 禁用,从 C- 借!
    			if(k2>=1&&a[i].a.w2==0){
    				REP(j,0,8) f[i+1][j][k2-1]=min(f[i+1][j][k2-1],g[i][j][k2-1]+a[i].a.len+(a[i].a.l!=j)*lll[a[i].a.l]+2);
    			}
    			if(k2<7&&a[i].a.w2==11){
    				REP(j,0,8) f[i+1][j][k2+1]=min(f[i+1][j][k2+1],g[i][j][k2+1]+a[i].a.len+(a[i].a.l!=j)*lll[a[i].a.l]+2);
    			}
    		}
    	}
    	
    	int mn=1e9,r1=0,r2=0;
    	REP(i,0,8) REP(j,0,8){
    		g[n][i][j]=f[n][i][j];
    		if(g[n][i][j]<mn) mn=g[n][i][j],r1=i,r2=j;
    	}
    	dfs(n,r1,r2); cout<<endl;
    }
    
    void TC() {
    	init();
    	int tc;cin>>tc;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    
    signed main() {
    	freopen("score.in","r",stdin);
    	freopen("score.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    

    7. [核心 Trick 与通用技巧](高亮警告)

    【多阶段分离决策 (Two-Phase DP / Switch-Execute DP)】 在处理含有“环境变量切换”的机器语言生成/压缩任务时,如果直接写 dp[step][state_out] = min(dp[step-1][state_in] + 切换代价 + 执行代价),由于内外层循环极多,很容易写出冗长且难以 debug 的代码。 这题演示了顶级的双阶段解耦 DP:

    1. 环境准备层 (g):g[i][new_s] = min(f[i][old_s] + 切换到 new_s 的代价)。表示我为了应对第 ii 个事件,强行把机器状态从 old_s 掰到 new_s 所花的最少代价。
    2. 事件执行层 (f):f[i+1][final_s] = min(g[i][cur_s] + 用 cur_s 状态执行事件并停留在 final_s 的代价)。 利用 f 阵与 g 阵交替跨步,不仅把思维复杂度降到了最低,还能在 O(1)O(1) 内极快地响应 dfs 回溯!这就是自动机理论中状态转移表的设计之美!
    • 0
      @ 2026-8-4 10:20:59

      乐谱:状态正交分离与完全背包降维的最短路规划

      基础信息:省选/NOI-(紫题) | 复杂度极限 O(∣S∣×64)O(|S| \times 64) | 编译原理级字符串解析、正交状态 DP、完全背包

      1. 这题在考啥 & 常规套路

      本题表面上是一个极其繁琐的字符串压缩问题,但其本质是带状态转移的最短路径规划 / DP 问题。 常规思维容易被大量的乐谱指令干扰,试图去写复杂的正则表达式或贪心替换。真正的制胜套路是**“底层逻辑提取”:我们不要去修改输入字符串,而是写一个编译器前端,将原乐谱执行一遍,解析出绝对纯净的事件流(Event Stream)**。然后利用 DP,从这个绝对事件流出发,在符合语法树的规则下,反向生成最短的字符串!

      2. 怎么想到的 (How & Why)

      暴力贪心的瓶颈在哪?如果我们只做局部替换(比如把 R4R4 换成 R2),会陷入“修改全局默认变量 L 带来的连锁反应”的深渊,导致无法兼顾全局最短。 哪个性质引导正解?正交状态分离!

      • 音量 V 绝对独立:改变音量的代价在它必须生效的音符前是固定的。它不影响时长、也不改变八度,所以 V 根本不需要加入 DP 状态!可以在回溯时贪心还原。
      • 长休止符 Rest 等价替换:因为 Rest 是可以无缝拼接的,所有的相邻 Rest 都可以在初期被无脑合并成一个巨大时长的“休止块”。 在这两个降维打击之后,剩余的全局状态只有两个:当前八度 O(1~8) 与 默认音长 L(共 8 种)。这就构成了极简的 8×8=648 \times 8 = 64 个状态的自动机图,我们可以直接在上面跑转移!

      3. 核心算法与“焊死”变量定义

      1. 编译器前端提取 (Parser):初始处于 o=4, l=4, v=100。遍历原字符串,解析出 Note(pitch, length, volume) 和 Rest(length) 事件,并将相邻 Rest 全部累加合并。长度全部统一映射到基础值:以 128 分音符为单位 1,则全音符为 128。
      2. 休止块的完全背包 (Rest DP):预处理出 dp_rest[W][l_in][l_out]。表示要凑出时长 WW,从默认音长 l_in 出发,并在完毕后留下 l_out 为全局音长的最小字符代价。
        • 大 WW 降维绝杀:当 WW 很大时,一定是切换到 L1 并疯狂使用隐式的 R(每次产生 128 时长,仅耗 1 字符,性价比称霸全场)。因此当 W>8000W > 8000 时,剥离出其 128128 的倍数强行插满 R,剩余余数复用预处理好的 DP 即可,规避了状态爆炸。
      3. 主旋律 DP 转移 (Main DP):
        • 焊死变量 main_dp[step][o_idx][l_idx]:表示前 step 个合并后的事件处理完,以 o_idx, l_idx 为结尾状态的最短字符串消耗。
        • 对于 Note 事件:穷举能够拼凑出其时长的 (n,dots)(n, \text{dots}) 搭配;穷举所有能在 1~8 八度内表示该绝对音高的自然音符 CDEFGAB(+|-),进行最短匹配的 DP 转移。
        • 考虑到 N≤105N \le 10^5,状态里绝对禁止携带 std::string!只存储能够唯一回溯出选择的 12 字节标量(动作 ID)。

      4. 时空复杂度与手推样例

      • 时间复杂度:解析 O(∣S∣)O(|S|);预处理 DP Rest 到 1000010000,复杂度 10000×8×8=O(K)10000 \times 8 \times 8 = O(K) 常数极小;主 DP 层数最大 10510^5,每层转移约 100 次,运算量 10710^7,在 3.0s 内游刃有余。
      • 空间复杂度:MainState 被极度压缩至 12 字节,使用 vector 开出 105×8×810^5 \times 8 \times 8,总消耗约为 76.8 MB,远低于题目慷慨的 1024 MB!

      5. 标程的工程构建与调试推演

      为多测清理环境:main_dp 使用 .assign() 动态重置,防御脏数据。 音高的相对还原:pitch = (o_out - 1) * 12 + note_idx。因为原乐谱不限制表达格式,我们可以算出其绝对值,在生成极简方案时,避开题目警告的“八度1禁用 C- / 八度8禁用 B+”。

      6. 考场必查的致命坑点

      • 字符串碎片化内存爆炸:如果在 DP 的同时去 + 拼接字符串,会导致运行中堆分配无数次短串,即便不 TLE 也会直接 MLE 退场!【强制】必须利用前驱指针(pre_o, pre_l, type)在最终只回溯生成 1 次完整字符串。
      • 附点时长的数学模型:nn 分音符带 kk 个附点,它的基础单位是 128/n128/n,带附点后总长为 (128/n/2k)×(2k+1−1)(128/n/2^k) \times (2^{k+1}-1)。注意这个除法不能有浮点误差,必须严格用整数域!

      7. 极简完整代码与 I/O 抉择

      采用快读与纯 C++ 组合以应付大量解析。

      #pragma GCC optimize("O3,unroll-loops")
      #include <bits/stdc++.h>
      using namespace std;
      
      const int INF = 1e9;
      
      int l_vals[] = {1, 2, 4, 8, 16, 32, 64, 128};
      inline int get_l_idx(int val) {
          for (int i = 0; i < 8; ++i) if (l_vals[i] == val) return i;
          return -1;
      }
      
      struct Event {
          int type; // 0: Note, 1: Rest
          int pitch;
          int L;
          int vol;
      };
      
      struct RestState {
          int cost;
          int pre_w;
          int pre_l;
          short type; // 0: Implicit R, 1: Explicit R, 2: L change
          short n;
          short dots;
      };
      
      RestState dp_rest[10005][8][8];
      
      struct NoteLenRep {
          int n, dots;
      };
      vector<NoteLenRep> reps_for[256];
      
      void precompute_rest() {
          for (int n_idx = 0; n_idx < 8; ++n_idx) {
              int n = l_vals[n_idx];
              for (int k = 0; k <= 7; ++k) {
                  if (n * (1 << k) <= 128) {
                      int L = (128 / n / (1 << k)) * ((1 << (k + 1)) - 1);
                      reps_for[L].push_back({n, k});
                  }
              }
          }
      
          for (int w = 0; w <= 10000; ++w)
              for (int i = 0; i < 8; ++i)
                  for (int j = 0; j < 8; ++j)
                      dp_rest[w][i][j] = {INF, -1, -1, 0, 0, 0};
      
          for (int i = 0; i < 8; ++i)
              dp_rest[0][i][i] = {0, 0, i, 0, 0, 0};
      
          for (int w = 1; w <= 10000; ++w) {
              for (int l_in = 0; l_in < 8; ++l_in) {
                  for (int l_out = 0; l_out < 8; ++l_out) {
                      for (int n_idx = 0; n_idx < 8; ++n_idx) {
                          int n = l_vals[n_idx];
                          for (int k = 0; k <= 7; ++k) {
                              if (n * (1 << k) <= 128) {
                                  int L = (128 / n / (1 << k)) * ((1 << (k + 1)) - 1);
                                  if (w >= L) {
                                      // 隐式
                                      if (l_vals[l_out] == n) {
                                          int cost = dp_rest[w - L][l_in][l_out].cost + 1 + k;
                                          if (cost < dp_rest[w][l_in][l_out].cost) {
                                              dp_rest[w][l_in][l_out] = {cost, w - L, l_out, 0, (short)n, (short)k};
                                          }
                                      }
                                      // 显式
                                      int cost = dp_rest[w - L][l_in][l_out].cost + 1 + (int)to_string(n).length() + k;
                                      if (cost < dp_rest[w][l_in][l_out].cost) {
                                          dp_rest[w][l_in][l_out] = {cost, w - L, l_out, 1, (short)n, (short)k};
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
              for (int l_in = 0; l_in < 8; ++l_in) {
                  for (int l_mid = 0; l_mid < 8; ++l_mid) {
                      if (dp_rest[w][l_in][l_mid].cost == INF) continue;
                      for (int l_out = 0; l_out < 8; ++l_out) {
                          if (l_mid == l_out) continue;
                          int cost = dp_rest[w][l_in][l_mid].cost + 1 + (int)to_string(l_vals[l_out]).length();
                          if (cost < dp_rest[w][l_in][l_out].cost) {
                              dp_rest[w][l_in][l_out] = {cost, w, l_mid, 2, (short)l_vals[l_out], 0};
                          }
                      }
                  }
              }
          }
      }
      
      string get_rest_str(int W, int l_in, int l_out) {
          if (W <= 10000) {
              string res = "";
              int cur_w = W, cur_l = l_out;
              while (cur_w > 0 || cur_l != l_in) {
                  auto& state = dp_rest[cur_w][l_in][cur_l];
                  string act = "";
                  if (state.type == 0) act = "R" + string(state.dots, '.');
                  else if (state.type == 1) act = "R" + to_string(state.n) + string(state.dots, '.');
                  else if (state.type == 2) act = "L" + to_string(state.n);
                  res = act + res;
                  cur_w = state.pre_w;
                  cur_l = state.pre_l;
              }
              return res;
          } else {
              int K = (W - 8000) / 128;
              int W_rem = W - K * 128;
              string res = "";
              int cur_w = W_rem, cur_l = l_out;
              bool inserted = false;
              while (cur_w > 0 || cur_l != l_in) {
                  auto& state = dp_rest[cur_w][l_in][cur_l];
                  string act = "";
                  if (state.type == 0) act = "R" + string(state.dots, '.');
                  else if (state.type == 1) act = "R" + to_string(state.n) + string(state.dots, '.');
                  else if (state.type == 2) act = "L" + to_string(state.n);
                  
                  if (!inserted && state.type == 0 && cur_l == 0) { // l_vals[0] = 1 (最高性价比触发点)
                      string extra = "";
                      for(int i = 0; i < K; ++i) extra += "R";
                      act = extra + act;
                      inserted = true;
                  }
                  res = act + res;
                  cur_w = state.pre_w;
                  cur_l = state.pre_l;
              }
              if (!inserted) {
                  string extra = "";
                  for(int i = 0; i < K; ++i) extra += "R";
                  res = extra + res; 
              }
              return res;
          }
      }
      
      int get_rest_cost(int W, int l_in, int l_out) {
          if (W <= 10000) return dp_rest[W][l_in][l_out].cost;
          int K = (W - 8000) / 128;
          int W_rem = W - K * 128;
          return K + dp_rest[W_rem][l_in][l_out].cost;
      }
      
      int get_o_cost(int o_in, int o_out) {
          if (o_in == o_out) return 0;
          int diff = o_out - o_in;
          if (abs(diff) == 1) return 1;
          return 2;
      }
      
      string get_o_transition_str(int o_in, int o_out) {
          if (o_in == o_out) return "";
          int diff = o_out - o_in;
          if (diff == 1) return ">";
          if (diff == -1) return "<";
          if (diff == 2) return ">>";
          if (diff == -2) return "<<";
          return "O" + to_string(o_out);
      }
      
      int get_l_cost(int l_in, int l_out) {
          if (l_in == l_out) return 0;
          return 1 + (int)to_string(l_vals[l_out]).length();
      }
      
      string get_l_transition_str(int l_in, int l_out) {
          if (l_in == l_out) return "";
          return "L" + to_string(l_vals[l_out]);
      }
      
      string get_note_name(int pitch, int o_out) {
          int base_pitch = (o_out - 1) * 12;
          int diff = pitch - base_pitch;
          string best = "";
          int nat[] = {0, 2, 4, 5, 7, 9, 11};
          string nat_name[] = {"C", "D", "E", "F", "G", "A", "B"};
          
          for (int i = 0; i < 7; ++i) {
              for (int offset = -1; offset <= 1; ++offset) {
                  if (nat[i] + offset == diff) {
                      if (o_out == 1 && i == 0 && offset == -1) continue;
                      if (o_out == 8 && i == 6 && offset == 1) continue;
                      string s = nat_name[i];
                      if (offset == -1) s += "-";
                      if (offset == 1) s += "+";
                      if (best == "" || s.length() < best.length()) {
                          best = s;
                      }
                  }
              }
          }
          return best;
      }
      
      struct MainState {
          int cost;
          short pre_o, pre_l;
          short is_rest;
          short cand_n, dots, is_explicit;
      };
      
      void solve() {
          string S;
          if (!(cin >> S)) return;
          
          vector<Event> events;
          int cur_o = 4, cur_l = 4, cur_v = 100;
          int i = 0;
          while (i < S.length()) {
              char c = S[i];
              if (c == 'O') {
                  cur_o = S[i+1] - '0';
                  i += 2;
              } else if (c == '<') {
                  cur_o = max(1, cur_o - 1); i++;
              } else if (c == '>') {
                  cur_o = min(8, cur_o + 1); i++;
              } else if (c == 'L') {
                  int val = 0; i++;
                  while (i < S.length() && isdigit(S[i])) { val = val * 10 + (S[i] - '0'); i++; }
                  cur_l = val;
              } else if (c == 'V') {
                  int val = 0; i++;
                  while (i < S.length() && isdigit(S[i])) { val = val * 10 + (S[i] - '0'); i++; }
                  cur_v = val;
              } else if (c == 'R') {
                  i++; int val = -1;
                  if (i < S.length() && isdigit(S[i])) {
                      val = 0;
                      while (i < S.length() && isdigit(S[i])) { val = val * 10 + (S[i] - '0'); i++; }
                  }
                  int dots = 0;
                  while (i < S.length() && S[i] == '.') { dots++; i++; }
                  int n = (val == -1) ? cur_l : val;
                  int L_val = (128 / n / (1 << dots)) * ((1 << (dots + 1)) - 1);
                  events.push_back({1, 0, L_val, 0});
              } else if (c >= 'A' && c <= 'G') {
                  int note_idx = 0;
                  if (c == 'C') note_idx = 0;
                  else if (c == 'D') note_idx = 2;
                  else if (c == 'E') note_idx = 4;
                  else if (c == 'F') note_idx = 5;
                  else if (c == 'G') note_idx = 7;
                  else if (c == 'A') note_idx = 9;
                  else if (c == 'B') note_idx = 11;
                  i++;
                  if (i < S.length() && (S[i] == '+' || S[i] == '-')) {
                      if (S[i] == '+') note_idx++; else note_idx--;
                      i++;
                  }
                  int pitch = (cur_o - 1) * 12 + note_idx;
                  int val = -1;
                  if (i < S.length() && isdigit(S[i])) {
                      val = 0;
                      while (i < S.length() && isdigit(S[i])) { val = val * 10 + (S[i] - '0'); i++; }
                  }
                  int dots = 0;
                  while (i < S.length() && S[i] == '.') { dots++; i++; }
                  int n = (val == -1) ? cur_l : val;
                  int L_val = (128 / n / (1 << dots)) * ((1 << (dots + 1)) - 1);
                  events.push_back({0, pitch, L_val, cur_v});
              }
          }
          
          vector<Event> merged;
          for (auto& ev : events) {
              if (merged.empty()) merged.push_back(ev);
              else {
                  if (ev.type == 1 && merged.back().type == 1) merged.back().L += ev.L;
                  else merged.push_back(ev);
              }
          }
          
          int num_events = merged.size();
          vector<vector<vector<MainState>>> main_dp;
          main_dp.assign(num_events + 1, vector<vector<MainState>>(8, vector<MainState>(8, {INF, -1, -1, 0, 0, 0, 0})));
          
          main_dp[0][3][2].cost = 0; // o=4 (idx=3), l=4 (idx=2)
          
          for (int step = 0; step < num_events; ++step) {
              if (merged[step].type == 1) { 
                  for (int o_in = 0; o_in < 8; ++o_in) {
                      for (int l_in = 0; l_in < 8; ++l_in) {
                          if (main_dp[step][o_in][l_in].cost == INF) continue;
                          for (int l_out = 0; l_out < 8; ++l_out) {
                              int cost = get_rest_cost(merged[step].L, l_in, l_out);
                              if (main_dp[step][o_in][l_in].cost + cost < main_dp[step+1][o_in][l_out].cost) {
                                  main_dp[step+1][o_in][l_out] = {
                                      main_dp[step][o_in][l_in].cost + cost,
                                      (short)o_in, (short)l_in,
                                      1, 0, 0, 0
                                  };
                              }
                          }
                      }
                  }
              } else {
                  int pitch = merged[step].pitch;
                  int L_val = merged[step].L;
                  for (int o_out = 0; o_out < 8; ++o_out) {
                      string name = get_note_name(pitch, o_out + 1);
                      if (name == "") continue;
                      
                      for (int o_in = 0; o_in < 8; ++o_in) {
                          int cost_O = get_o_cost(o_in + 1, o_out + 1);
                          for (int l_in = 0; l_in < 8; ++l_in) {
                              if (main_dp[step][o_in][l_in].cost == INF) continue;
                              
                              for (auto& cand : reps_for[L_val]) {
                                  // 隐式
                                  int l_out_impl = get_l_idx(cand.n);
                                  int cost_L = get_l_cost(l_in, l_out_impl);
                                  int total = cost_O + cost_L + (int)name.length() + cand.dots;
                                  if (main_dp[step][o_in][l_in].cost + total < main_dp[step+1][o_out][l_out_impl].cost) {
                                      main_dp[step+1][o_out][l_out_impl] = {
                                          main_dp[step][o_in][l_in].cost + total,
                                          (short)o_in, (short)l_in,
                                          0, (short)cand.n, (short)cand.dots, 0
                                      };
                                  }
                                  
                                  // 显式
                                  int total_ex = cost_O + 0 + (int)name.length() + (int)to_string(cand.n).length() + cand.dots;
                                  if (main_dp[step][o_in][l_in].cost + total_ex < main_dp[step+1][o_out][l_in].cost) {
                                      main_dp[step+1][o_out][l_in] = {
                                          main_dp[step][o_in][l_in].cost + total_ex,
                                          (short)o_in, (short)l_in,
                                          0, (short)cand.n, (short)cand.dots, 1
                                      };
                                  }
                              }
                          }
                      }
                  }
              }
          }
          
          int min_cost = INF, best_o = -1, best_l = -1;
          for (int o = 0; o < 8; ++o) {
              for (int l = 0; l < 8; ++l) {
                  if (main_dp[num_events][o][l].cost < min_cost) {
                      min_cost = main_dp[num_events][o][l].cost;
                      best_o = o; best_l = l;
                  }
              }
          }
          
          vector<string> actions(num_events);
          int curr_o = best_o, curr_l = best_l;
          for (int i = num_events; i > 0; --i) {
              auto& state = main_dp[i][curr_o][curr_l];
              if (state.is_rest) {
                  actions[i-1] = get_rest_str(merged[i-1].L, state.pre_l, curr_l);
              } else {
                  string str_O = get_o_transition_str(state.pre_o + 1, curr_o + 1);
                  string name = get_note_name(merged[i-1].pitch, curr_o + 1);
                  if (!state.is_explicit) {
                      string str_L = get_l_transition_str(state.pre_l, curr_l);
                      actions[i-1] = str_O + str_L + name + string(state.dots, '.');
                  } else {
                      actions[i-1] = str_O + name + to_string(state.cand_n) + string(state.dots, '.');
                  }
              }
              curr_o = state.pre_o;
              curr_l = state.pre_l;
          }
          
          string final_res = "";
          int current_v = 100;
          for (int i = 0; i < num_events; ++i) {
              if (merged[i].type == 0) { 
                  if (merged[i].vol != current_v) {
                      final_res += "V" + to_string(merged[i].vol);
                      current_v = merged[i].vol;
                  }
              }
              final_res += actions[i];
          }
          cout << final_res << "\n";
      }
      
      int main() {
          freopen("score.in", "r", stdin);
          freopen("score.out", "w", stdout);
          ios_base::sync_with_stdio(false);
          cin.tie(NULL);
          precompute_rest();
          int t;
          if (cin >> t) {
              while (t--) solve();
          }
          return 0;
      }
      

      8. [核心 Trick 与通用技巧](高亮警告)

      【数据解耦抽象 (Data-Instruction Decoupling) 与图论重构】 这是一切涉及“规则冗长、操作花哨但最终要求执行效率或重组压缩”问题的唯一神技! 无论原题给了多么绕的规则(如修改某个环境因子,然后隐式触发变化),千万别在原字符串上做正则替换或局部贪心! 【第一定律】:直接写一个全量解析器,将过程状态直接拍平,转化成拥有绝对常量的底层事件流数组 (Event Stream)。 【第二定律】:梳理各个环境因子对事件的正交性。只要一个状态的变化只在某特定类型事件强制生效,且修改成本已知(如本题的 Volume),即可彻底踢出 DP 的状态空间,强行给状态维数降秩。这会把原本 O(N×Vmax×L×O)O(N \times V_{max} \times L \times O) 的四维空间直接暴降为二维的 O(N×L×O)O(N \times L \times O),瞬间绝杀!

      • 0
        @ 2026-7-27 16:05:54
        /*
         *                                                     __----~~~~~~~~~~~------___
         *                                    .  .   ~~//====......          __--~ ~~
         *                    -.            \_|//     |||\\  ~~~~~~::::... /~
         *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
         *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
         *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
         *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
         *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
         *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
         *           '         ~-|      /|    |-~\~~       __--~~
         *                       |-~~-_/ |    |   ~\_   _-~            /\
         *                            /  \     \__   \/~                \__
         *                        _--~ _/ | .-~~____--~-/                  ~~==.
         *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
         *                                  -_     ~\      ~~---l__i__i__i--~~_/
         *                                  _-~-__   ~)  \--______________--~~
         *                                //.-~~~-~_--~- |-------~~~~~~~~
         *                                       //.-~~~--\
         *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         *
         *                               神兽保佑            永无BUG
         */
        
        /*
         * @Description: I want to be the weakest vegetable in the world!
         * @Author: I.Z.
         */
        #include<bits/stdc++.h>
        using namespace std;
        #define MOD         998244353
        #define speMOD      2933256077ll
        #define pii         pair<int,int>
        #define all(v)      v.begin(),v.end()
        #define pb          push_back
        #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
        #define over(x)     {cout<<(x)<<endl;return;}
        #define lowbit(x)   ((x)&(-(x)))
        #define cntbit(x)   __builtin_popcount(x)
        #define rf(v)       shuffle(all(v),sd);
        #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
        #define lbound(v,x) lower_bound(all(v),x)-v.begin()
        mt19937 sd(random_device{}());
        int qpow(int a,int b,int m=MOD,int res=1){
        	a%=m;
        	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
        	return res;
        }
        string YN(bool x,string Y="Yes",string N="No") {
        	if(x){
        		return Y;
        	} else {
        		return N;
        	}
        }
        int exgcd(int x,int y,int &a,int &b){
        	if(y==0){
        		a=1;b=0;
        		return x;
        	}
        	int d=exgcd(y,x%y,a,b);
        	int z=b;
        	b=a-b*(x/y);
        	a=z;
        	return d;
        }
        int _x_,_y_;
        inline int inverse(int x,int y=MOD){
        	exgcd(x,y,_x_,_y_);
        	return (_x_+y)%y;
        }
        int fac[2000005],inv[2000005];
        void init(int n){
        	fac[0]=inv[0]=1;
        	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
        	inv[n]=qpow(fac[n],MOD-2);
        	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
        }
        int binom(int x,int y){
        	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
        }
        map<string,int>mp;
        int Len[200][200];
        int comb[8][8][2600];
        int lll[8]={3,2,2,2,1,1,1,1};
        int llen[12]={1,2,1,2,1,1,2,1,2,1,2,1};
        string S[12]={"C","D-","D","E-","E","F","F+","G","A-","A","B-","B"};
        struct note{//音符,包括:周期,编号,长度 
        	int w1,w2;int l,len;
        	void out(int x){
        		cout<<S[w2];
        		if(x!=l)cout<<128/(1<<l);
        		REP(i,0,len)cout<<'.';
        	}
        };
        note tonote(string s,int id=4,int L=4){
        	--id;
        //	cout<<s<<endl;
        	if(s.size()>=2&&s[0]=='B'&&s[1]=='+'){
        		++id;
        		s="C"+s.substr(2,s.size()-2);
        	}else if(s.size()>=2&&s[0]=='C'&&s[1]=='-'){
        		--id;
        		s="B"+s.substr(2,s.size()-2);
        	}
        	int len=1;
        	if(s.size()>1&&(s[1]=='+'||s[1]=='-'))len=2;
        	note a;
        	a.w1=id;a.w2=mp[s.substr(0,len)];
        	s=s.substr(len,s.size()-len);
        	int t=0;
        //	cout<<s<<endl;
        	if(!s.size()||!isdigit(s[0])){
        		a.l=__lg(128/L);a.len=s.size();
        	}else{
        		int cnt=0;
        		REP(i,0,s.size())if(isdigit(s[i]))t=t*10+s[i]-48;
        		else ++cnt;
        		t=128/t;a.l=__lg(t);a.len=cnt;
        	}
        //	cout<<a.w1<<' '<<a.w2<<' '<<a.l<<' '<<a.len<<endl;
        	return a;
        }
        int dp[8][8][2600];//长度够 1280 就塞 L1RRRRRRRRRR
        tuple<int,int,int>from[8][8][2600];
        void output(int x,int y,int s){
        //	cout<<'!'<<x<<' '<<y<<' '<<s<<' '<<Len[0][0]<<endl;system("pause");
        	if(!s){
        		if(x!=y)cout<<'L'<<128/(1<<y);
        		return;
        	}
        	auto [i,j,k]=from[x][y][s];
        	cout<<'R';
        	if(i!=x)cout<<128/(1<<i);
        	REP(l,0,j)cout<<'.';
        	if(k!=x)cout<<'L'<<128/(1<<k);
        //	cout<<i<<' '<<j<<' '<<Len[0][0]<<endl;system("pause");
        	output(k,y,s-Len[i][j]);
        }
        struct Rest{
        	int a;
        	void init(string s,int L=4){
        		s=s.substr(1,s.size()-1);
        		if(s.size()&&isdigit(s[0])){
        			int lst=0;L=0;
        			REP(i,0,s.size())if(isdigit(s[i]))++lst,L=L*10+s[i]-48;
        			s=s.substr(lst,s.size()-lst);
        		}
        		L=128/L;
        		int cur=L;
        		REP(i,0,s.size())L+=cur/=2;
        		a=L;
        	}
        	int calc(int x,int y){
        		if(a<=2560)return dp[x][y][a];
        		int b=a%1280+1280;
        		return (a-b)/128+comb[x][y][b];
        	}
        	void out(int x,int y){
        //		cout<<x<<' '<<y<<' '<<a<<endl;system("pause");
        		if(a<=2560){
        			output(x,y,a);
        		}else{
        			int b=a%1280+1280;
        			int t1=-1,t2=-1,t3=-1;
        //			cout<<'!'<<b<<endl;
        			REP(i,0,2561)if(i<=b){
        				REP(j,0,8){
        					REP(k,0,8){
        						if(comb[x][y][b]==dp[x][j][i]+(j!=7)*2+(k!=7)*(1+lll[k])+dp[k][y][b-i]){
        							t1=j;t2=k;t3=i;break;
        						}
        					}
        					if(t1!=-1)break;
        				}
        				if(t1!=-1)break;
        			}
        //			cout<<t1<<' '<<t2<<' '<<t3<<' '<<dp[t2][y][b-t3]<<endl;system("pause");
        			output(x,t1,t3);if(t1!=7)cout<<"L1";
        			REP(i,0,(a-b)/128)cout<<'R';
        			if(t2!=7)cout<<'L'<<128/(1<<t2);
        			output(t2,y,b-t3);
        		}
        	}
        };
        struct large{
        	int op;
        	note a;Rest b;
        };
        int mn1[2600][8],mn2[2600][8];
        void init() {
        	mp["B+"]=mp["C"]=0;mp["C+"]=mp["D-"]=1;
        	mp["D"]=2;mp["D+"]=mp["E-"]=3;
        	mp["E"]=mp["F-"]=4;mp["F"]=mp["E+"]=5;
        	mp["F+"]=mp["G-"]=6;mp["G"]=7;
        	mp["G+"]=mp["A-"]=8;mp["A"]=9;
        	mp["A+"]=mp["B-"]=10;mp["B"]=mp["C-"]=11;
        	REP(i,0,8){
        		int cur=(1<<i);
        		REP(j,0,i+1){
        			cur|=(1<<(i-j));
        			Len[i][j]=cur;
        		}
        	}
        	REP(i,0,8){
        		REP(j,0,8)dp[i][j][0]=i==j? 0:1+lll[j];
        	}
        	REP(len,1,2561){
        		REP(s1,0,8){
        			REP(s2,0,8){
        				dp[s1][s2][len]=1e9;
        				//接下来塞 (1<<i),i[0,7], 后面塞 <=i 个附点
        				REP(i,0,8){
        					REP(j,0,i+1)if(Len[i][j]<=len){
        						REP(p,0,8){
        							int t=1+j+(i!=s1)*lll[i]+(s1!=p)*(1+lll[p])+dp[p][s2][len-Len[i][j]];
        							if(t<=dp[s1][s2][len])dp[s1][s2][len]=t,from[s1][s2][len]={i,j,p};
        						}
        					}
        				}
        			}
        		}
        	}
        //    REP(i,1,2561){
        //        REP(j,0,8){
        //            REP(k,0,8)cout<<dp[j][k][i]<<' ';
        //        }
        //        cout<<endl;
        //    }
        	REP(i,0,2561){
        		REP(s1,0,8){
        			int mn=1e9;
        			REP(s2,0,8)mn=min(mn,dp[s1][s2][i]+(s2!=7)*2);
        			mn1[i][s1]=mn;
        			mn=1e9;
        			REP(s2,0,8)mn=min(mn,dp[s2][s1][i]+(s2!=7)*(1+lll[s2]));
        			mn2[i][s1]=mn;
        		}
        	}
        	REP(i,0,2561){
        		REP(j,0,8){
        			REP(k,0,8)comb[j][k][i]=1e9;
        		}
        	}
        	REP(i,0,2561){
        		REP(s1,0,8){
        			REP(j,0,2561)if(i+j<=2560){
        				REP(s2,0,8){
        					comb[s1][s2][i+j]=min(comb[s1][s2][i+j],mn1[i][s1]+mn2[j][s2]);
        				}
        			}
        		}
        	}
        }
        int calcO(int x,int y){return x==y? 0:min(2,abs(x-y));}
        int calcL(int x,int y){return x==y? 0:1+lll[y];}
        int f[100005][8][8],g[100005][8][8];
        vector<large>a;
        int ps1[100005],ps2[100005];
        int Vl[100005];
        int curV;
        void dfs(int x,int s1,int s2){
        	ps1[x-1]=s1;ps2[x-1]=s2;
        	int si=0,sj=0;
        	REP(i,0,8){
        		REP(j,0,8){
        			if(g[x][s1][s2]==f[x][i][j]+calcO(j,s2)+calcL(i,s1))si=i,sj=j;
        		}
        	}
        	if(x){
        		--x;
        		if(a[x].op){
        			int J=-1;
        			REP(j,0,8)if(g[x][j][sj]+a[x].b.calc(j,si)==f[x+1][si][sj]){J=j;break;}
        			dfs(x,J,sj);a[x].b.out(J,si);
        		}else{
        			if(a[x].a.w1==sj){
        				dfs(x,si,sj);
        				if(Vl[x]!=curV)cout<<'V'<<(curV=Vl[x]);
        				a[x].a.out(si);
        			}else if(a[x].a.w1-1==sj){
        				dfs(x,si,sj);
        				if(Vl[x]!=curV)cout<<'V'<<(curV=Vl[x]);
        				cout<<"B+";
        				if(a[x].a.l!=si)cout<<128/(1<<a[x].a.l);
        				REP(i,0,a[x].a.len)cout<<'.';
        			}else{
        				dfs(x,si,sj);
        				if(Vl[x]!=curV)cout<<'V'<<(curV=Vl[x]);
        				cout<<"C-";
        				if(a[x].a.l!=si)cout<<128/(1<<a[x].a.l);
        				REP(i,0,a[x].a.len)cout<<'.';
        			}
        		}
        	}
        	if(si!=s1)cout<<'L'<<(128/(1<<s1));
        	if(sj!=s2){
        		if(sj+1==s2)cout<<'>';
        		else if(sj-1==s2)cout<<'<';
        		else cout<<'O'<<s2+1;
        	}
        //	cout<<' ';
        }
        int tc;
        void Main(){
        	string s;
        	cin>>s;
        	int L=4,O=4,V=100;
        	a.clear();
        	REP(i,0,s.size()){
        		if(s[i]=='>')++O;
        		else if(s[i]=='<')--O;
        		else if(isalpha(s[i])){
        			int j=i+1;
        			while(j<(int)s.size()&&(!isalpha(s[j])&&s[j]!='<'&&s[j]!='>'))++j;
        			--j;
        			string t=s.substr(i,j-i+1);
        			if(t[0]=='O'){
        				O=t[1]-48;
        			}else if(t[0]=='L'){
        				t=t.substr(1,t.size()-1);
        				L=stoll(t);
        			}else if(t[0]=='R'){
        				Rest x;x.init(t,L);
        				if(a.size()&&a.back().op){
        					a.back().b.a+=x.a;
        				}else a.pb(large{1,{},x});
        			}else if(t[0]=='V'){
        				t=t.substr(1,t.size()-1);
        				V=stoll(t);
        			}else{
        				a.pb(large{0,tonote(t,O,L),{}});
        				Vl[a.size()-1]=V;
        			}
        			i=j;
        		}
        	}
        	curV=100;
        	REP(i,0,8){
        		REP(j,0,8)f[0][i][j]=1e9;
        	}
        	f[0][5][3]=0;
        	int n=a.size();
        	REP(i,0,n){
        		REP(j,0,8)REP(k,0,8)f[i+1][j][k]=g[i][j][k]=1e9;
        		REP(j,0,8){
        			REP(k,0,8){
        				REP(j2,0,8){
        					REP(k2,0,8)g[i][j2][k2]=min(g[i][j2][k2],f[i][j][k]+calcO(k,k2)+calcL(j,j2));
        				}
        			}
        		}
        		if(a[i].op){
        			REP(j,0,8){
        				REP(j2,0,8){
        					REP(k,0,8)f[i+1][j2][k]=min(f[i+1][j2][k],g[i][j][k]+a[i].b.calc(j,j2));
        				}
        			}
        		}else{
        			int k2=a[i].a.w1;
        			if(a[i].a.w1<8&&a[i].a.w1>=0){
        				REP(j,0,8){
        					f[i+1][j][k2]=min(f[i+1][j][k2],g[i][j][k2]+a[i].a.len+(a[i].a.l!=j)*lll[a[i].a.l]+llen[a[i].a.w2]);
        				}
        			}
        			if(k2>=1&&a[i].a.w2==0){
        				REP(j,0,8){
        					f[i+1][j][k2-1]=min(f[i+1][j][k2-1],g[i][j][k2-1]+a[i].a.len+(a[i].a.l!=j)*lll[a[i].a.l]+2);
        				}
        			}
        			if(k2<7&&a[i].a.w2==11){
        				REP(j,0,8){
        					f[i+1][j][k2+1]=min(f[i+1][j][k2+1],g[i][j][k2+1]+a[i].a.len+(a[i].a.l!=j)*lll[a[i].a.l]+2);
        				}
        			}
        		}
        	}
        	int mn=1e9,r1=0,r2=0;
        	REP(i,0,8){
        		REP(j,0,8){
        			g[n][i][j]=f[n][i][j];
        			if(g[n][i][j]<mn)mn=g[n][i][j],r1=i,r2=j;
        		}
        	}
        //	cout<<a[0].b.a<<endl;system("pause");
        //	cout<<mn<<' '<<r1<<' '<<r2<<endl;
        	dfs(n,r1,r2);cout<<endl;
        }
        void TC() {
        	init();
        	int tc;cin>>tc;
        	while(tc--){
        		Main();
        		cout.flush();
        	}//cerr<<clock()<<endl;
        }
        signed main() {
        	freopen("score.in","r",stdin);
        	freopen("score.out","w",stdout);
        	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
        }
        /*
        1. CLEAR the arrays (ESPECIALLY multitests)
        2. DELETE useless output
         */
        
        • 1

        Information

        ID
        2848
        Time
        3000ms
        Memory
        1024MiB
        Difficulty
        10
        Tags
        (None)
        # Submissions
        2
        Accepted
        2
        Uploaded By