Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static int reitinPituus(String reitti) {
- HashSet <Integer> pisteet = new HashSet<>();
- int pituus = reitti.length();
- int x = 0;
- int y = 0;
- Piste piste1 = new Piste(x, y);
- int hashluku1 = piste1.hashCode();
- pisteet.add(hashluku1);
- int askeleet = 0;
- for (int i = 0; i < pituus; i++) {
- if (reitti.charAt(i) == 'O') {
- x += 1;
- } else if (reitti.charAt(i) == 'V') {
- x -= 1;
- } else if (reitti.charAt(i) == 'A') {
- y -= 1;
- } else {
- y += 1;
- }
- Piste piste = new Piste(x, y);
- int hashluku = piste.hashCode();
- if (pisteet.contains(hashluku)) {
- askeleet = i + 1;
- break;
- }
- pisteet.add(hashluku);
- }
- return askeleet;
- }
- public static void main(String[] args) {
- System.out.println(reitinPituus("YYVVAAOO"));
- System.out.println(reitinPituus("YVAOYVAO"));
- System.out.println(reitinPituus("YYYYYYYY"));
- System.out.println(reitinPituus("OYVVAOOO"));
- System.out.println(reitinPituus("YYYAAA"));
- }
- }
- public class Piste {
- private int x;
- private int y;
- public Piste (int x, int y) {
- this.x = x;
- this.y = y;
- }
- public int getPisteX() {
- return this.x;
- }
- public int getPisteY() {
- return this.y;
- }
- public String toString() {
- return this.x + " ja " + this.y;
- }
- public boolean equals(Object olio) {
- if (olio == null) {
- return false;
- }
- if (getClass() != olio.getClass()) {
- return false;
- }
- Piste verrattava = (Piste) olio;
- if (this.x != verrattava.x) {
- return false;
- }
- if (this.y != verrattava.y) {
- return false;
- }
- return true;
- }
- @Override
- public int hashCode() {
- int hash = 3;
- hash = 37 * hash + this.x;
- hash = 37 * hash + this.y;
- return hash;
- }
- }
- TEST PACKAGES
- import org.junit.Test;
- import static org.junit.Assert.*;
- import fi.helsinki.cs.tmc.edutestutils.Points;
- import fi.helsinki.cs.tmc.edutestutils.timing.CpuStopwatch;
- import java.util.Arrays;
- @Points("5.4")
- public class MainTest {
- public void pieniTesti(String reitti, int tulos) {
- int uusi = Main.reitinPituus(reitti);
- assertTrue("Reitin " + reitti + " pituuden tulisi olla " + tulos +
- ", mutta metodisi antaa " + uusi + ".",
- tulos == uusi);
- }
- public void suuriTesti(String reitti, int tulos) {
- CpuStopwatch kello=new CpuStopwatch(CpuStopwatch.Mode.USER);
- int uusi = Main.reitinPituus(reitti);
- assertTrue("Metodisi toimii väärin suurella syötteellä.",
- tulos == uusi);
- double aika=kello.getElapsedTime();
- assertTrue("Metodisi kuluttaa liikaa aikaa: "+kello+"s, kun aikaraja on 1s.",
- aika<=1);
- }
- @Test(timeout=5000)
- public void esimerkit() {
- pieniTesti("YYVVAAOO", 8);
- pieniTesti("YVAOYVAO", 4);
- pieniTesti("YYYYYYYY", 0);
- pieniTesti("OYVVAOOO", 6);
- }
- @Test(timeout=5000)
- public void pienet() {
- pieniTesti("Y", 0);
- pieniTesti("A", 0);
- pieniTesti("V", 0);
- pieniTesti("O", 0);
- pieniTesti("YY", 0);
- pieniTesti("AA", 0);
- pieniTesti("VV", 0);
- pieniTesti("OO", 0);
- pieniTesti("YA", 2);
- pieniTesti("AY", 2);
- pieniTesti("VO", 2);
- pieniTesti("OV", 2);
- pieniTesti("YV", 0);
- pieniTesti("AV", 0);
- pieniTesti("OY", 0);
- pieniTesti("OA", 0);
- pieniTesti("VYOA", 4);
- pieniTesti("OAVY", 4);
- pieniTesti("AOYV", 4);
- pieniTesti("AVYO", 4);
- pieniTesti("YYYAAA", 4);
- pieniTesti("YYYVVV", 0);
- pieniTesti("YYOOAVVVAO", 7);
- pieniTesti("OOYYOAOYYY", 0);
- pieniTesti("OOYYOAAVVV", 8);
- }
- @Test(timeout=5000)
- public void suuri1() {
- int n = 100000;
- char[] mjono = new char[n];
- for (int i = 0; i < n; i++) mjono[i] = 'A';
- suuriTesti(new String(mjono), 0);
- }
- @Test(timeout=5000)
- public void suuri2() {
- int n = 100000;
- char[] mjono = new char[n];
- for (int i = 0; i < n/2; i++) mjono[i] = 'A';
- for (int i = n/2; i < n; i++) mjono[i] = 'Y';
- suuriTesti(new String(mjono), n/2+1);
- }
- @Test(timeout=5000)
- public void suuri3() {
- int n = 100000;
- char[] mjono = new char[n];
- for (int i = 0; i < n/4; i++) mjono[i] = 'A';
- for (int i = n/4; i < n/2; i++) mjono[i] = 'O';
- for (int i = n/2; i < n/2+n/4; i++) mjono[i] = 'Y';
- for (int i = n/2+n/4; i < n; i++) mjono[i] = 'V';
- suuriTesti(new String(mjono), n);
- }
- @Test(timeout=5000)
- public void suuri4() {
- int n = 100000;
- char[] mjono = new char[n];
- long x = 12345;
- long a = 798732191;
- long b = 921339221;
- for (int i = 0; i < n/3; i++) {
- x = (x*a)%b;
- if (x%2 == 0) mjono[i] = 'O';
- if (x%2 == 1) mjono[i] = 'Y';
- }
- mjono[n/3] = 'O';
- for (int i = n/3+1; i < n/2; i++) {
- mjono[i] = 'A';
- }
- mjono[n/2] = 'V';
- for (int i = n/2+1; i < n; i++) {
- x = (x*a)%b;
- if (x%2 == 0) mjono[i] = 'V';
- if (x%2 == 1) mjono[i] = 'Y';
- }
- suuriTesti(new String(mjono), 66746);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement