Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program z9;
- {$APPTYPE CONSOLE}
- {$R+}
- uses
- SysUtils;
- type
- un = record
- s: string;
- o: boolean;
- end;
- var
- s, res: string;
- a: array[0..200001] of un;
- i, c, x, y, z, w, sp, fn: integer;
- function func(x, m: integer): integer;
- begin
- if a[x].s <> '?' then
- func := length(a[x].s)
- else
- func := m;
- end;
- function len(m: integer): integer;
- var
- i, fn: integer;
- rs: int64;
- begin
- rs := func(1, m);
- for i := 2 to c do begin
- fn := func(i, m);
- if a[i - 1].o then
- rs := fn * rs + rs
- else
- rs := rs + fn;
- if rs > maxint then begin
- len := maxint;
- exit;
- end;
- end;
- len := rs;
- end;
- function bin(n: integer): integer;
- var
- l, r, m, x, ln: integer;
- begin
- l := 1;
- r := n;
- x := length(res);
- while r > l do begin
- m := (l + r) div 2;
- ln := len(m);
- if ln < x then
- l := m + 1
- else
- if ln > x then
- r := m
- else begin
- bin := m;
- exit;
- end;
- end;
- bin := l;
- end;
- begin
- reset(input, 'input.txt');
- rewrite(output, 'output.txt');
- readln(s);
- res := '';
- i := 1;
- c := 0;
- while s[i] <> '=' do begin
- if not (s[i] in ['*', '+']) then
- res := res + s[i]
- else begin
- inc(c);
- a[c].s := res;
- res := '';
- if s[i] = '*' then
- a[c].o := true
- else
- a[c].o := false;
- end;
- inc(i);
- end;
- a[0].o := true;
- a[c + 1].o := true;
- inc(c);
- a[c].s := res;
- res := copy(s, i + 1, length(s) - i);
- x := bin(length(res));
- i := 1;
- while a[i].s <> '?' do
- inc(i);
- y := i;
- dec(i);
- while (i >= 1) and a[i].o do
- dec(i);
- z := i;
- i := y;
- while (i <= c) and a[i].o do
- inc(i);
- w := i;
- sp := 0;
- for i := y + 1 to c do
- if a[i - 1].o then
- if (sp = 0) then
- sp := func(i, x)
- else
- sp := (sp + 1) * func(i, x) + sp;
- if (w = y) and ((z + 1) = w) and ((z + 1) = y) then
- if a[1].s = '?' then begin
- fn := 1;
- for i := 1 to x do begin
- write(res[fn]);
- inc(fn, sp + 1)
- end;
- halt(0);
- end
- else begin
- w := length(a[1].s);
- for i := 2 to y - 1 do
- if a[i - 1].o then
- w := w * length(a[i].s) + w
- else
- w := length(a[i].s) + w;
- for i := y + 1 to c do
- if a[i - 1].o then
- w := w * func(i, x) + w;
- fn := 1;
- for i := 1 to x do begin
- write(res[w + fn]);
- inc(fn, sp + 1);
- end;
- halt(0);
- end;
- if a[1].s = '?' then begin
- fn := 1;
- for i := 1 to x do begin
- write(res[fn]);
- inc(fn, sp + 1);
- end;
- halt(0);
- end;
- if a[y - 1].o then begin
- fn := 1;
- for i := 1 to x do begin
- write(res[fn + sp + 1]);
- inc(fn, sp + 1);
- end;
- halt(0);
- end;
- z := length(a[1].s);
- for i := 2 to y - 1 do
- if a[i - 1].o then
- z := z * length(a[i].s) + z
- else
- z := z + length(a[i].s);
- for i := y + 1 to c do
- if a[i - 1].o then
- z := z * func(i, x) + z;
- fn := 1;
- for i := 1 to x do begin
- write(res[z + fn]);
- inc(fn, sp + 1);
- end;
- close(input);
- close(output);
- end.
Add Comment
Please, Sign In to add comment