Is the Name of This Problem: Difference between revisions
imported>Kmk21 No edit summary |
imported>Kmk21 m Kmk21 moved page Quine to Is the Name of This Problem |
(No difference)
|
Revision as of 19:42, 13 January 2016
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();
}
}