Is the Name of This Problem: Difference between revisions
imported>Tlw37 No edit summary |
imported>Kmk21 No edit summary |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
==Problem | ==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 | ||
Line 9: | Line 9: | ||
==== Solution - Java ==== | ==== Solution - Java ==== | ||
<syntaxhighlight line lang="java"> | <syntaxhighlight line lang="java"> | ||
import java.util.Scanner; | import java.util.Scanner; | ||
public class | public class quine { | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
Scanner scan = new Scanner(System.in); | Scanner scan = new Scanner(System.in); | ||
Line 63: | Line 62: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==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 | |||
[[Category:mcpc2012]] | [[Category:mcpc2012]] | ||
[[Category: | [[Category:ICPC Problems]] | ||
[[Category:Algorithm Easy]] | [[Category:Algorithm Easy]] | ||
[[Category:Implementation Easy]] | [[Category:Implementation Easy]] | ||
[[Category:Regex]] | |||
[[Category:Strings]] |
Latest revision as of 19:46, 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();
}
}
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