P1928 外星密码

题目描述

有了防护伞,并不能完全避免 2012 的灾难。地球防卫小队决定去求助外星种族的帮 助。经过很长时间的努力,小队终于收到了外星生命的回信。但是外星人发过来的却是一 串密码。只有解开密码,才能知道外星人给的准确回复。解开密码的第一道工序就是解压 缩密码,外星人对于连续的若干个相同的子串“X”会压缩为“[DX]”的形式(D 是一个整 数且 1≤D≤99),比如说字符串“CBCBCBCB”就压缩为“[4CB]”或者“[2[2CB]]”,类 似于后面这种压缩之后再压缩的称为二重压缩。如果是“[2[2[2CB]]]”则是三重的。现 在我们给你外星人发送的密码,请你对其进行解压缩。

输入格式

第一行:一个字符串

输出格式

第一行:一个字符串

输入输出样例

输入 #1复制

AC[3FUN]

输出 #1复制

ACFUNFUNFUN

说明/提示

【数据范围】

对于 50%的数据:解压后的字符串长度在 1000 以内,最多只有三重压缩。

对于 100%的数据:解压后的字符串长度在 20000 以内,最多只有十重压缩。 对于 100%的数据:保证只包含数字、大写字母、’[‘和’]‘

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<stack>
#include<cctype>
using namespace std;
char str[2005];
void solve(int L,int R){
	if(L-1==R) return;
	int p=L,pl=0,pr=R;//定义游标p 左右端点 
	while(p<=R&&str[p]>='A'&&str[p]<='Z'){
		printf("%c",str[p++]);
	}
	//找出【    】 的左右下标 pl pr
	pl=p;
	if(p-1==R) return;//到头了就返回 
	stack<char> st;
	while(p<=R){
		if(str[p]=='[') st.push('[');
		if(str[p]==']') {
			st.pop();
			if(st.empty()) {
				pr=p;
				//printf("%d %d",pl,pr);
				break;
			}	
		}
		p++;
	}
	p=pl+1; int num=0;
	if(isdigit(str[p])) num=num*10+str[p++]-'0';
	if(isdigit(str[p])) num=num*10+str[p++]-'0';
	//printf("%d ",num);
	if(num!=0){
		for(int i=0;i<num;i++){
			solve(p,pr-1);
		}	
	}	
	solve(pr+1,R);
}
int main() {
	//freopen("1.in","r",stdin);   
   // freopen("~~题目名称~~.out","w",stdout);
	cin>>str;
	int len=strlen(str);
	solve(0,len-1);
	return 0;
}
//本程序没有考虑这种样例,注意made后的内容没有输出 THIS[10DATA[7IS]]MADE[8BY[9NETTLE]]
#include <bits/stdc++.h>
using namespace std;
char str[20002];
int p=0;
void dfs(int L){//[12ac[3fu[2n]]]   ac[3fu[2n]]
 	p=L; int repeat;
	if(str[p]!='[') {
		repeat=1;
	}else{
		repeat=0; p++;
		if(isdigit(str[p])){repeat=repeat*10+str[p++]-'0';	}
		if(isdigit(str[p])){repeat=repeat*10+str[p++]-'0';	}
	}
//以上代码为了算出重复次数repeat和起点位置p 
//cout<<"\nrepeat"<<repeat<<"pos"<<str[p]<<endl;
	int pos=p;
	while(repeat--){
		p=pos;
		while(isalpha(str[p])) {
			printf("%c",str[p]);
			p++;
		}
		if(str[p]=='[') {
			dfs(p);
			while(isalpha(str[p])) {
				printf("%c",str[p]);
				p++;
			}
		}else if(str[p]==']') {
			p++;
			//return;
		}
	}
} 
int main(){
	freopen("1.txt","r",stdin);
    scanf("%s",str);
//    cout<<str<<endl;
    dfs(p);
    return 0;
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值