3、外星密码(password.???)

3、外星密码(password.???)

 

问题描述:

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

现在我们将给你外星人发送的密码,请你对其进行解压缩。

 

输入格式:

一行一个字符串。

 

输出格式:

一行一个字符串。

 

输入样例:

AC[3FUN]

 

输出样例:

ACFUNFUNFUN

 

数据限制:

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

对于100%的数据:解压后的字符串长度在20000以内,最多只有十重压缩;

保证只包含数字、大写字母、’[’和’]’。

【自己题解】

var
  i,j,n,m,s,c,x:longint;
  a,b:ansistring;
  f:boolean;
procedure pin(s:string);
begin
  assign(input,s+'.in');
  reset(input);
  assign(output,s+'.out');
  rewrite(output);
end;
procedure pout;
begin
  close(input);
  close(output);
end;
begin
  pin('password');
  readln(a);
  c:=pos(']',a);
  while (c<>0) do
  begin
    inc(x);
    b:='';
    s:=0;
    f:=false;
    for i:=c-1 downto 1 do
    begin
      if a[i]='[' then
      break;
      if (a[i]>='0')and(a[i]<='9') then
      begin
        if f=false then
        begin
          f:=true;
          s:=s+(ord(a[i])-48);
        end
        else
        s:=s+(ord(a[i])-48)*10;
      end
      else
      b:=a[i]+b;
    end;
    delete(a,i,c-i+1);
    if s=0 then
    s:=1;
    for j:=1 to s do
    insert(b,a,i);
    c:=pos(']',a);
  end;
  writeln(a);
  pout;
end.

猜你喜欢

转载自blog.csdn.net/shenyixuanruoruo/article/details/85540080