Is the Name of This Problem: Difference between revisions

From programming_contest
Jump to navigation Jump to search
imported>Tlw37
Created page with "==Introduction== The big media gimmick these days seems to be vampires. Vampire movies, vampire television shows, vampire books, vampire dolls, vampire cereal, vampire lipsti..."
 
imported>Kmk21
No edit summary
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Introduction==
==Problem Statement==
 
http://speedyguy17.info/data/mcpc/mcpc2012/pdf/D-Is%20the%20Name%20of%20This%20Problem.pdf
The big media gimmick these days seems to be vampires. Vampire movies, vampire television shows,
vampire books, vampire dolls, vampire cereal, vampire lipstick, vampire bunnies – kids and teenagers
and even some adults spend lots of time and money on vampire-related stuff. Surprisingly, nowadays
vampires are often the good guys. Obviously, the ACM Programming Contest had better have a vampire
problem in order to be considered culturally relevant.
As eveyone knows, vampires are allergic to garlic, sunlight, crosses, wooden stakes, and the Internal
Revenue Service. But curiously they spend a good part of their time smashing mirrors. Why? Well,
mirrors can’t hurt vampires physically, but it’s embarrassing to be unable to cast a reflection. Mirrors
hurt vampire’s feelings. This problem is about trying to help them avoid mirrors.
In a room full of vampires and ordinary mortals there are a number of mirrors. Each mirror has one
of four orientations – north, south, east, or west (the orientation indicates which side of the mirror
reflects). A vampire is in danger of embarrassment if he or she is in a direct horizontal or vertical line
with the reflecting side of a mirror, unless there are intervening objects (mortals or other mirrors). Your job is to notify each vampire of the directions in which there is danger of experiencing
ENR (embarrassing non-reflectivity).
[http://www.example.com link title]
[[Vampires]]
 


==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".
There are a few methods of solving this problem. The simplest way is to create a 2D grid where each point can be a wall, a vampire, a mortal, or a mirror. These 'points' can either be their own objects or can be mapped to a single integer. Then, we simply iterate through every vampire and go in each cardinal direction and see if any mirrors reflect back to it (ignoring all the irrelevant objects).  
Note: A much simpler solution is possible using regular expressions.


===Code===
===Code===
==== Solution - Java ====
==== Solution - Java ====
<syntaxhighlight line lang="java">
<syntaxhighlight line lang="java">
package accepted;
import java.util.Scanner;
import java.util.Scanner;


public class Problem_AL_Is_the_Name_of_This_Problem {
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 81: Line 63:
</syntaxhighlight>
</syntaxhighlight>


[[Category:ecna2010]]
==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:ICPC Problems]]
[[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