Algorithm


Problrm link- https://www.spoj.com/problems/ABSYS/

ABSYS - Anti-Blot System

no tags 

 

Jimmy is a hard-working pupil in his second year at primary school. Recently he decided to convert all his notes into an electronic version. Sadly, he found that his math notes were full of ink blots.

He scanned the notes and sent them through his own OCR package (yes, he coded it all by himself at the age of 8). The OCR package replaced all ink blots by the string "machula".

Problem specification

You are given Jimmy's notes, processed by the OCR. They contain simple math exercises, which were used to practice addition on positive integers. Your task is to recover the damaged part of the notes.

Input specification

The first line of the input file contains an integer T specifying the number of test cases. Each test case is preceded by a blank line.

Each test case consists of exactly one line. The line represents an equation of the form "number + number = number", where each number is a positive integer. One part of the equation will be replaced by the string "machula". The string always covers a contiguous non-empty sequence of digits, possibly even an entire number. You may assume that for each equation in the input there will be exactly one way to fill in the missing digits.

Output specification

For each test case, the output shall contain one line of the form "number + number = number". The line must represent the equation from that test case with all missing digits filled in.

Example

Input:
3

23 + 47 = machula

3247 + 5machula2 = 3749

machula13 + 75425 = 77038

Output:
23 + 47 = 70
3247 + 502 = 3749
1613 + 75425 = 77038

Note: int in C++/C/Java or longint in Pascal is enough.

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming

import java.io.*;
import java.util.*;
import java.math.*;

class A{
	public static void main(String[] args){
		InputReader in = new InputReader(System.in);
		OutputWriter out = new OutputWriter(System.out);
		int t = in.readInt();
		Solver solveA = new Solver();
		while(t-- > 0){
		    solveA.solve(in, out);
		}
		out.flush();
		out.close();
	}
}
class Solver{
	final int mod = (int)1e9+7;
	void solve(InputReader in, OutputWriter out){
        String input1, input2, input3, input4, input5;
        input1 = in.readString();
        input2 = in.readString();
        input3 = in.readString();
        input4 = in.readString();
        input5 = in.readString();
        if(input1.contains("machula")){
            out.printLine(Integer.parseInt(input5)-Integer.parseInt(input3)+" "+input2+" "+input3+" "+input4+" "+input5);
        }else if(input3.contains("machula")){
            out.printLine(input1+" "+input2+" "+(Integer.parseInt(input5)-Integer.parseInt(input1))+" "+input4+" "+input5);
        }else{
            out.printLine(input1+" "+input2+" "+input3+" "+input4+" "+(Integer.parseInt(input1)+Integer.parseInt(input3)));
        }
    }
}
class InputReader
{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream)
    {
        this.stream = stream;
    }

    public int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars)
        {
            curChar = 0;
            try
            {
                numChars = stream.read(buf);
            } catch (IOException e)
            {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int readInt()
    {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-')
        {
            sgn = -1;
            c = read(>;
        }
        int res = 0;
        do
        {
            if (c  <  '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public String readString()
    {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do
        {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }
    public double readDouble() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        double res = 0;
        while (!isSpaceChar(c) && c != '.') {
            if (c == 'e' || c == 'E')
                return res * Math.pow(10, readInt());
            if (c  <  '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        }
        if (c == '.') {
            c = read();
            double m = 1;
            while (!isSpaceChar(c)) {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, readInt());
                if (c  <  '0' || c > '9')
                    throw new InputMismatchException();
                m /= 10;
                res += (c - '0') * m;
                c = read();
            }
        }
        return res * sgn;
    }
    public long readLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long res = 0;
        do {
            if (c  <  '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }
    public boolean isSpaceChar(int c)
    {
        if (filter != null)
            return filter.isSpaceChar(c);
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public String next()
    {
        return readString();
    }

    public interface SpaceCharFilter
    {
        public boolean isSpaceChar(int ch);
    }
}

class OutputWriter
{
    private  PrintWriter writer;

    public OutputWriter(OutputStream outputStream)
    {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer)
    {
        this.writer = new PrintWriter(writer);
    }

    public void print(Object... objects)
    {
        for (int i = 0; i  <  objects.length; i++)
        {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    public void printLine(Object... objects)
    {
        print(objects);
        writer.println();
    }

    public void close()
    {
        writer.close();
    }

    public void flush()
    {
        writer.flush();
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
23 + 47 = machula
3247 + 5machula2 = 3749
machula13 + 75425 = 77038

Output

x
+
cmd
23 + 47 = 70
3247 + 502 = 3749
1613 + 75425 = 77038

#2 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include <string>
#include <locale>

using namespace std;
bool allDigit(string str){
	for(int i=0;i<str.length();++i){
		if(isalpha(str[i]))
			return false;
	}	
	return true;
}
int main() {
	int t;
	cin>>t;
	while(t--){
		string op1,op2,plus,eq,op3;
		int result;
		cin>>op1>>plus>>op2>>eq>>op3;
		
		if(allDigit(op1) && allDigit(op2)){
			result = stoi(op2)+stoi(op1);
			cout<<op1<<" + "<<op2<<" = "<<result<<endl;
		}
		if(allDigit(op1) && allDigit(op3)){
			result = stoi(op3)-stoi(op1);
			cout<<op1<<" + "<<result<<" = "<<op3<<endl;
		}
		if(allDigit(op2) && allDigit(op3)){
			result = stoi(op3)-stoi(op2);
			cout<<result<<" + "<<op2<<" = "<<op3<<endl;
		}
	}
	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
23 + 47 = machula
3247 + 5machula2 = 3749
machula13 + 75425 = 77038

Output

x
+
cmd
23 + 47 = 70
3247 + 502 = 3749
1613 + 75425 = 77038
Advertisements

Demonstration


SPOJ Solution-Anti-Blot System-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python