Created
June 22, 2018 23:03
-
-
Save wheelerlaw/821e3a0a2a3faed2fd8cd457de00b6c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class DetectSubsequence{ | |
public static void main(String[] args){ | |
Scanner sc = new Scanner(System.in); | |
int[] substring = new int[sc.nextInt()]; | |
int[] data = new int[sc.nextInt()]; | |
for(int i=0; i<substring.length; i++){ | |
substring[i] = sc.nextInt(); | |
} | |
for(int i=0; i<data.length; i++){ | |
data[i] = sc.nextInt(); | |
} | |
int substringIndex = 0; | |
for(int i=0; i<data.length; i++){ | |
if(substringIndex == substring.length) | |
break; | |
if(data[i] == substring[substringIndex]) | |
substringIndex++; | |
} | |
if(substringIndex == substring.length){ | |
System.out.println("YES"); | |
}else{ | |
System.out.println("NO"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import static java.lang.Math.abs; | |
public class PaperFold { | |
public static void main(String[] args){ | |
Coordinate[] coords = PaperFold.getInput(true); | |
Line[] lines = new Line[(coords.length*(coords.length-1))/2]; | |
// Calculate: O(n^2) | |
int i = 0; | |
for(int j = 0; j < coords.length; j++){ | |
for(int k = j + 1; k < coords.length; k++){ | |
Coordinate coord1 = coords[j]; | |
Coordinate coord2 = coords[k]; | |
if(coord1.equals(coord2)) continue; | |
float b = (coord2.y*coord2.y - coord1.y*coord1.y + coord2.x*coord2.x - coord1.x*coord1.x)/(2*(coord2.y - coord1.y)); | |
float m = abs(-((coord2.x - coord1.x)/(coord2.y - coord1.y))); | |
if(m == Float.NEGATIVE_INFINITY || m == Float.POSITIVE_INFINITY){ | |
b = coord2.x - coord1.x; | |
} | |
lines[i] = new Line(m, b); | |
i++; | |
} | |
} | |
// Sort: O(n log n), | |
MergeSort.sort(lines); | |
// Count: O(n) | |
Line prev = lines[0]; | |
int count = 1; | |
int max = 1; | |
for(int j = 1; j < lines.length; j++){ | |
if(lines[j].equals(prev)) count++; | |
else{ | |
if(count>max) max = count; | |
prev = lines[j]; | |
count = 1; | |
} | |
} | |
if(count>max) max = count; | |
System.out.println(max); | |
} | |
public static Coordinate[] getInput(boolean multiple){ | |
Scanner sc = new Scanner(System.in); | |
Coordinate[] input; | |
int length = Integer.parseInt(sc.nextLine().trim()); | |
input = new Coordinate[length]; | |
for(int i=0; i<input.length; i++){ | |
String[] line = sc.nextLine().trim().split(" "); | |
input[i] = new Coordinate(Integer.parseInt(line[0]), Integer.parseInt(line[1])); | |
} | |
sc.close(); | |
return input; | |
} | |
public static class MergeSort{ | |
public static void sort(Line[] lines){ | |
sort(lines, 0, lines.length); | |
} | |
private static void sort(Line[] lines, int low, int high){ | |
int length = high - low; | |
if(length <= 1) | |
return; | |
int middle = low + length/2; | |
sort(lines, low, middle); | |
sort(lines, middle, high); | |
Line[] temp = new Line[length]; | |
int i = low, j = middle; | |
for(int k = 0; k < length; k++){ | |
if(i == middle){ | |
temp[k] = lines[j++]; | |
}else if(j == high){ | |
temp[k] = lines[i++]; | |
}else if(lines[j].lessThan(lines[i])){ | |
temp[k] = lines[j++]; | |
}else{ | |
temp[k] = lines[i++]; | |
} | |
} | |
for(int k = 0; k < length; k++){ | |
lines[low + k] = temp[k]; | |
} | |
} | |
} | |
public static class Coordinate{ | |
public final float x; | |
public final float y; | |
public Coordinate(int x, int y){ | |
this.x = x; | |
this.y = y; | |
} | |
public Coordinate(float x, float y){ | |
this.x = x; | |
this.y = y; | |
} | |
public boolean equals(Object o){ | |
if(o instanceof Coordinate){ | |
Coordinate coord = (Coordinate)o; | |
if(coord.x == this.x && coord.y == this.y){ | |
return true; | |
} | |
} | |
return false; | |
} | |
public String toString(){ | |
return "("+this.x+", "+this.y+")"; | |
} | |
} | |
public static class Line{ | |
public final float m; | |
public final float b; | |
public Line(float slope, float yIntercept){ | |
this.m = slope; | |
this.b = yIntercept; | |
} | |
public boolean equals(Object o){ | |
if(o instanceof Line){ | |
Line line = (Line)o; | |
if(line.b == this.b && line.m == this.m){ | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* True if the CALLING OBJECT is less than or equal to the PARAMETER | |
* @param line | |
* @return | |
*/ | |
public boolean lessThan(Line line){ | |
if(this.m < line.m) return true; | |
if(this.m == line.m && this.b < line.b) return true; | |
return false; | |
} | |
public String toString(){ | |
return "y = "+this.m+"x + "+this.b; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment