Is the Name of This Problem: Difference between revisions

From programming_contest
Jump to navigation Jump to search
imported>Tlw37
imported>Tlw37
Line 1: Line 1:
==Problem AL==
==Problem Statement==
http://speedyguy17.info/data/mcpc/mcpc2012/pdf/D-Is%20the%20Name%20of%20This%20Problem.pdf
http://speedyguy17.info/data/mcpc/mcpc2012/pdf/D-Is%20the%20Name%20of%20This%20Problem.pdf



Revision as of 03:43, 5 December 2015

Problem Statement

http://speedyguy17.info/data/mcpc/mcpc2012/pdf/D-Is%20the%20Name%20of%20This%20Problem.pdf

Idea

First, we read in the next line and check to make sure that the first character is a quotation mark. We then continue to iterate through the string until we reach another quotation mark, and save the string that came between the quotes. We then check if the next character is an empty space. Finally, we save the rest of the string starting from the current index (1 after the blank space). We then check to make sure the two strings are equal. If so, we print Quine(A), where A represents one of the equivalent strings. If at any point in this process one of the conditions is not met, we immediately print "not a quine" and jump to processing the next line. Input is ended by a line containing the string "END". Note: A much simpler solution is possible using regular expressions.

Code

Solution - Java

import java.util.Scanner;

public class quine {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		while (true) {
			String input = scan.nextLine();
			String inQuotes = "", outOfQuotes = "";
			int index = 0;

			if (input.equals("END"))
				break;

			if (input.charAt(index++) != '"') {
				System.out.println("not a quine");
				continue;
			}

			boolean endOfString = false;
			while (true) {
				if (index + 1 >= input.length()) {
					endOfString = true;
					break;
				}

				char c = input.charAt(index++);
				if (c == '"') {
					break;
				}

				inQuotes += c;
			}

			if (endOfString || input.charAt(index) != ' ') {
				System.out.println("not a quine");
				continue;
			}

			if (index + 1 < input.length()) {
				outOfQuotes = input.substring(++index);
			}

			if (inQuotes.equals(outOfQuotes)) {
				System.out.println("Quine(" + inQuotes + ")");
			} else {
				System.out.println("not a quine");
			}
		}
		scan.close();
	}
}