Monday, 24 February 2014

UVA Problem ID 442 (Matrix Chain Multiplication)

UVA Problem ID 442 (Matrix Chain Multiplication)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;


public class Main {
	public static void main (String args[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb=new StringBuilder(1000);
		int no_of_matrices=Integer.parseInt(br.readLine());
		Matrix matrices[]=new Matrix[26];
		for (int i = 0; i  st=new Stack();
			int totalcost=0;
			for (int i = 0; i < input.length(); i++) {
				if(input.charAt(i)=='(')
				{
					//do nothing
				}
				else if(input.charAt(i)==')')
				{
					
					Matrix b=(Matrix) st.pop();
					Matrix a=(Matrix) st.pop();

					if(a.column!=b.row)
					{
						sb.append("error\n");
						flag=false;
						break;
					}
					else
					{
						totalcost+=a.row*a.column*b.column;
						st.push(new Matrix('t',a.row,b.column));		
					}
				}
				else
				{
					st.push(matrices[input.charAt(i)-65]);
				}
			}
			if(st.size()>2)
			{
				sb.append("error\n");
				flag=false;
			}
			else if(!st.isEmpty() && st.size()==2)
			{
				
				Matrix b=(Matrix) st.pop();
				Matrix a=(Matrix) st.pop();	
				if(a.column!=b.row)
				{
					sb.append("error\n");
					flag=false;
				}
				else
				{
					totalcost+=a.row*a.column*b.column;				
				}
			}
			if(flag)
				sb.append(totalcost+"\n");
		}
		System.out.print(sb);
	}
}
class Matrix
{
	public int row;
	public int column;
	public char name;
	
	public Matrix(char name,int row,int column)
	{
		this.name=name;
		this.row=row;
		this.column=column;
	}
}

Saturday, 25 January 2014

UVa Problem ID 673 (Parentheses Balance)

UVa Problem ID 673 (Parentheses Balance)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String input;
		StringBuffer sb = new StringBuffer("");
		int testcases = Integer.parseInt(br.readLine());

		for (int i = 0; i < testcases; i++)
		{
			boolean iscorrect = true;
			input = br.readLine();
			Stack st = new Stack();
			for (int j = 0; j < input.length(); j++)
			{
				if (iscorrect)
				{
					switch (input.charAt(j))
					{
						case '(':
						{
							st.push('(');
							break;
						}
						case '[':
						{
							st.push('[');
							break;
						}
						case ']':
						{
							if(st.isEmpty())
							{
								iscorrect=false;
								break;
							}
							char c = st.pop();
							if (c != '[')
							{
								iscorrect = false;
							}
							break;
						}
						case ')':
						{
							if(st.isEmpty())
							{
								iscorrect=false;
								break;
							}
							char c = st.pop();
							if (c != '(')
							{
								iscorrect = false;
							}
							break;
						}
					}
				}
			}
			if(iscorrect && st.isEmpty())
				sb.append("Yes");
			else
				sb.append("No");
			sb.append("\n");
		}
		System.out.print(sb);
	}

}

Advertisement