Is the Name of This Problem: Difference between revisions

From programming_contest
Jump to navigation Jump to search
imported>Tlw37
imported>Tlw37
Line 4: Line 4:


==Idea==
==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".
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===
===Code===
Line 65: Line 66:


[[Category:mcpc2012]]
[[Category:mcpc2012]]
[[Category:ICPC Problems]]
[[Category:MCPC Problems]]
[[Category:Algorithm Easy]]
[[Category:Algorithm Easy]]
[[Category:Implementation Easy]]
[[Category:Implementation Easy]]

Revision as of 03:34, 5 December 2015

Problem

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

package accepted;
import java.util.Scanner;

public class Problem_AL_Is_the_Name_of_This_Problem {
	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();
	}
}