Is the Name of This Problem

From programming_contest
Jump to: navigation, search

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

 1 import java.util.Scanner;
 2 
 3 public class quine {
 4 	public static void main(String[] args) {
 5 		Scanner scan = new Scanner(System.in);
 6 
 7 		while (true) {
 8 			String input = scan.nextLine();
 9 			String inQuotes = "", outOfQuotes = "";
10 			int index = 0;
11 
12 			if (input.equals("END"))
13 				break;
14 
15 			if (input.charAt(index++) != '"') {
16 				System.out.println("not a quine");
17 				continue;
18 			}
19 
20 			boolean endOfString = false;
21 			while (true) {
22 				if (index + 1 >= input.length()) {
23 					endOfString = true;
24 					break;
25 				}
26 
27 				char c = input.charAt(index++);
28 				if (c == '"') {
29 					break;
30 				}
31 
32 				inQuotes += c;
33 			}
34 
35 			if (endOfString || input.charAt(index) != ' ') {
36 				System.out.println("not a quine");
37 				continue;
38 			}
39 
40 			if (index + 1 < input.length()) {
41 				outOfQuotes = input.substring(++index);
42 			}
43 
44 			if (inQuotes.equals(outOfQuotes)) {
45 				System.out.println("Quine(" + inQuotes + ")");
46 			} else {
47 				System.out.println("not a quine");
48 			}
49 		}
50 		scan.close();
51 	}
52 }

Idea 2: Regex

We simply have to determine if this matches a regex. The regex is:

^\"([A-Za-z ]+)\" \1

The java implementation allows you to extract the group out of the regex matching afterwards