%AUTHOR : MYSTICVEGETA aka Tan
%CCC PROBLEM 4
var fi, fo : int
open : fi, "DATA2.txt", get
open : fo, "OUT2.txt", put
var n : int
var line : string
var fArray : flexible array 1 .. 0 of string
var main : flexible array 1 .. 0 of string
get : fi, n
proc splitToArray (l : string)
var sen := l
loop
var ind := index (sen, " ")
new fArray, upper (fArray) + 1
fArray (upper (fArray)) := sen (1 .. ind - 1)
sen := sen (ind + 1 .. *)
exit when index (sen, " ") = 0
end loop
new fArray, upper (fArray) + 1
fArray (upper (fArray)) := sen (1 .. *)
end splitToArray
fcn mulOrDiv (ar : array 1 .. * of string) : boolean
for x : 1 .. upper (ar)
if (ar (x) = "X") or (ar (x) = "/") then
result true
end if
end for
result false
end mulOrDiv
fcn retCon (ar : array 1 .. * of string) : boolean
var p := 0
var s := "X/+-"
for x : 1 .. upper (ar)
if (index (s, ar (x)) > 0) then %(ar (x) (1) not= "(") and (ar (x) (*) not= ")") and (index (s, ar (x)) <= 0) then
p += 1
end if
end for
if p <= 1 then
result true
else
result false
end if
end retCon
proc solve (arr : array 1 .. * of string)
var pList := ""
if not (mulOrDiv (arr)) then
pList := "+-"
else
pList := "X/"
end if
% for x : 1 .. upper (arr)
% put arr (x) + " " ..
% end for
% put ""
% Input.Pause
for x : 1 .. upper (arr)
if index (pList, arr (x)) > 0 then
var stored := ""
stored := "(" + arr (x - 1) + " " + arr (x) + " " + arr (x + 1) + ")"
for y : 1 .. upper (arr)
if (y not= x) and (y not= x + 1) then
new main, upper (main) + 1
if (y = x - 1) then
main (upper (main)) := stored
else
main (upper (main)) := arr (y)
end if
end if
end for
exit
end if
end for
free fArray
for x : 1 .. upper (main)
new fArray, x
fArray (x) := main (x)
end for
end solve
for x : 1 .. n
get : fi, line : *
var ans := ""
splitToArray (line (1 .. *))
loop
solve (fArray)
free main
exit when retCon (fArray)
end loop
for i : 1 .. upper (fArray)
ans += fArray (i) + " "
end for
put : fo, ans (1 .. * -1)
free fArray
end for
|