跳到正文

个人记录

pragma 题解

RedLycoris约 14 分钟
原文
文章信息与版本

文章信息

发布
2020年9月26日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 14 分钟

版本

  1. v2当前
  2. v1

U120928 题目链接

此题用来讽刺洛谷禁止#pragma预编译命令,还直接子串匹配。 ——By 神铃

这篇是模仿铃酱的

试图用BrainFuck做出这题

考虑到 Ural 1552 Brainfuck是道很不错的dp题

然后这题让你输出一个最短的Brainfuck程序,使得它能输出输入的字符串

于是我用了我这题AC的程序:

C++
#include<bits/stdc++.h>
using namespace std;
const int mxn=2e5+5;
short dp[2][28][28][28][4],n;
pair<pair<char,char>,char>par[52][28][28][28][4];
inline int G(char c){
	return c-'a'+1;
}
inline int D(int x,int y){
	if(x==0)return y+96;
	else return abs(y-x);
}
inline int Get(char c){
	if(('a'<=c and c<='z'))return c-'a'+1;
	return 0;
}
inline void solve(){
	string s;cin>>s;s=" "+s;n=s.size();
	memset(dp,63,sizeof(dp));int inf=dp[1][0][0][0][0];
	dp[1][0][0][0][0]=0,dp[1][0][0][0][1]=0,dp[1][0][0][0][2]=0,dp[1][0][0][0][3]=0;
	for(int i=1;i<n;++i){
		memset(dp[(i+1)&1],63,sizeof(dp[(i+1)&1]));
		for(int x=0;x<=27;++x){
			for(int y=0;y<=27;++y){
				for(int z=0;z<=27;++z){
					for(int l=0;l<4;++l){
						int a,b,c,d;
						if(dp[i&1][x][y][z][l]==inf)continue;
						if(l==0)a=Get(s[i-1]),b=x,c=y,d=z;
						if(l==1)a=x,b=Get(s[i-1]),c=y,d=z;
						if(l==2)a=x,b=y,c=Get(s[i-1]),d=z;
						if(l==3)a=x,b=y,c=z,d=Get(s[i-1]);
//						cerr<<s[i-1]<<' '<<x<<' '<<y<<' '<<z<<' '<<l<<" : "<<a<<' '<<b<<' '<<c<<' '<<d<<'\n';
						//1
						if(dp[i&1][x][y][z][l]+D(a,G(s[i]))+abs(l-0)<dp[(i+1)&1][b][c][d][0]){
							dp[(i+1)&1][b][c][d][0]=dp[i&1][x][y][z][l]+D(a,G(s[i]))+abs(l-0);
							par[i+1][b][c][d][0]={{l,a},0};
						}
						//2
						if(dp[i&1][x][y][z][l]+D(b,G(s[i]))+abs(l-1)<dp[(i+1)&1][a][c][d][1]){
							dp[(i+1)&1][a][c][d][1]=dp[i&1][x][y][z][l]+D(b,G(s[i]))+abs(l-1);
							par[i+1][a][c][d][1]={{l,b},1};
						}
						//3
						if(dp[i&1][x][y][z][l]+D(c,G(s[i]))+abs(l-2)<dp[(i+1)&1][a][b][d][2]){
							dp[(i+1)&1][a][b][d][2]=dp[i&1][x][y][z][l]+D(c,G(s[i]))+abs(l-2);
							par[i+1][a][b][d][2]={{l,c},2};
						}
						//4
						if(dp[i&1][x][y][z][l]+D(d,G(s[i]))+abs(l-3)<dp[(i+1)&1][a][b][c][3]){
							dp[(i+1)&1][a][b][c][3]=dp[i&1][x][y][z][l]+D(d,G(s[i]))+abs(l-3);
							par[i+1][a][b][c][3]={{l,d},3};
						}
					}
				}
			}
		}
	}
	int mn=32222;
	int A,B,C,D,L,X,Y,Z;
	for(int x=0;x<=27;++x){
		for(int y=0;y<=27;++y){
			for(int z=0;z<=27;++z){
				for(int l=0;l<4;++l){
					if(dp[n&1][x][y][z][l]<mn){
						mn=dp[n&1][x][y][z][l];
						X=x,Y=y,Z=z,L=l;
						if(l==0)A=Get(s[n-1]),B=x,C=y,D=z;
						if(l==1)A=x,B=Get(s[n-1]),C=y,D=z;
						if(l==2)A=x,B=y,C=Get(s[n-1]),D=z;
						if(l==3)A=x,B=y,C=z,D=Get(s[n-1]);
					}
				}
			}
		}
	}
//	cerr<<mn<<'\n';
	string ans="";
	for(int i=n;i>1;--i){
		int Lp=par[i][X][Y][Z][L].first.first,pos=par[i][X][Y][Z][L].second,pre=par[i][X][Y][Z][L].first.second;
		if(L==0)A=Get(s[i-1]),B=X,C=Y,D=Z;
		if(L==1)A=X,B=Get(s[i-1]),C=Y,D=Z;
		if(L==2)A=X,B=Y,C=Get(s[i-1]),D=Z;
		if(L==3)A=X,B=Y,C=Z,D=Get(s[i-1]);
//		cerr<<Lp<<' '<<pos<<' '<<pre<<' '<<L<<' '<<A<<' '<<B<<' '<<C<<' '<<D<<'\n';
		ans+='.';
		if(pos==0){
			if(A>pre){
				for(int i=pre;i<A;++i)ans+='+';
				if(pre==0)for(int i=0;i<96;++i)ans+='+';
			}else for(int i=A;i<pre;++i)ans+='-';
			if(Lp>0)for(int i=0;i<Lp;++i)ans+='<';
			else for(int i=Lp;i<0;++i)ans+='>';
			A=pre;
		}
		if(pos==1){
			if(B>pre){
				for(int i=pre;i<B;++i)ans+='+';
				if(pre==0)for(int i=0;i<96;++i)ans+='+';
			}else for(int i=B;i<pre;++i)ans+='-';
			if(Lp>1)for(int i=1;i<Lp;++i)ans+='<';
			else for(int i=Lp;i<1;++i)ans+='>';
			B=pre;
		}
		if(pos==2){
			if(C>pre){
				if(pre==0)for(int i=0;i<96;++i)ans+='+';
				for(int i=pre;i<C;++i)ans+='+';
			}else for(int i=C;i<pre;++i)ans+='-';
			if(Lp>2)for(int i=2;i<Lp;++i)ans+='<';
			else for(int i=Lp;i<2;++i)ans+='>';
			C=pre;
		}
		if(pos==3){
			if(D>pre){
				if(pre==0)for(int i=0;i<96;++i)ans+='+';
				for(int i=pre;i<D;++i)ans+='+';
			}else for(int i=D;i<pre;++i)ans+='-';
			if(Lp>3)for(int i=3;i<Lp;++i)ans+='<';
			else for(int i=Lp;i<3;++i)ans+='>';
			D=pre;
		}
		if(Lp==0)X=B,Y=C,Z=D;
		if(Lp==1)X=A,Y=C,Z=D;
		if(Lp==2)X=A,Y=B,Z=D;
		if(Lp==3)X=A,Y=B,Z=C;
		L=Lp;
	}
	reverse(ans.begin(),ans.end());
	cout<<ans<<'\n';
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int T=1;//cin>>T;
	for(;T--;)solve();
}

输入了 pragma

然后输出了这个:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++.-----------------.++++++.++++++.------------.

然后用网上的BrainFuck解释器套一下

就这样了:

C++
#include <iostream>
#include <stack>
#include <cstdio>
#include <map>
#include <sstream>
using	namespace	std;

const	int	SIZE = 300000;

bool	is_instruction(char);
string Code="++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++.-----------------.++++++.++++++.------------.";
int	main(void)
{

//	while(1)
//	{
		char	instruction[SIZE];
		char	ch;
		int	count = 0;stringstream ss(Code);
		while(ss>>ch)			//????,????????????
			if(is_instruction(ch))
				instruction[count ++] = ch;
		instruction[count] = '\0';

		stack<int>	left_bracket_stack;
		map<int,int>	another_bracket_at;
		bool	instruction_ok = true;

		for(int i = 0;instruction[i] != '\0';i ++)	//????????,??????
		{
			if(instruction[i] == '[')
				left_bracket_stack.push(i);
			else	if(instruction[i] == ']')
			{
				if(left_bracket_stack.empty())
				{
					instruction_ok = false;
					break;
				}
				int	left_barcket_index = left_bracket_stack.top();;
				left_bracket_stack.pop();
				another_bracket_at[i] = left_barcket_index;
				another_bracket_at[left_barcket_index] = i;
			}
		}
		if(!left_bracket_stack.empty())
			instruction_ok = false;
		if(!instruction_ok)				//????????????
		{
			puts("????");
			return 0;
		}


//		cout << endl << "***** BEGIN *****" << endl << endl;

		int	i = 0;
		char	box[SIZE] = {0};
		char	* cur = box;
		while(instruction[i] != '\0')
		{
			if(instruction[i] == '>')
				cur ++;
			else	if(instruction[i] == '<')
			{
				cur --;
				if(cur < box)			//??????????????
				{
					puts("????");
					break;
				}
			}
			else	if(instruction[i] == '+')
				++ (*cur);
			else	if(instruction[i] == '-')
				-- (*cur);
			else	if(instruction[i] == '.')
				putchar(*cur);
			else	if(instruction[i] == ',')
				*cur = getchar();
			else	if(instruction[i] == '[')
			{
				if(*cur == 0)
					i = another_bracket_at[i];
			}
			else	if(instruction[i] == ']')
				if(*cur)
					i = another_bracket_at[i];
			i ++;
		}

//		cout << endl << endl << "***** DONE *****" << endl << endl;
//	}

	return 0;
}

bool	is_instruction(char ch)
{
	if(ch == '>' || ch == '<' || ch == '+' || ch == '-' || ch == '.' || ch == ',' || ch == '[' || ch == ']')
		return	true;
	return	false;
}

即可AC