0

Need help with this task. How can i Compare line two and line three where line three need to be the reverse off line two to print out succeeded?

input:

The first line of the input contains a single integer 1≤≤20. The two following lines each contain a string containing only the characters 0 and 1. The first of these lines represent the bits of the file before deletion and the second the bits on the same position on the hard drive after the file has been deleted. The length of these strings are the same and between 1 and 1000 characters.

output:

Output a single line containing either the words “Deletion succeeded” if each bit is switched times or “Deletion failed” if this is not the case.

So the input will be something like this:

1
10001110101000001111010100001110
01110001010111110000101011110001

and if line two and three i the same(just det third line is reversed from line two) then the output will be:

Deletion succeeded

if input is this:

 20
 0001100011001010
 0001000011000100

Where line two and three is not the same, then the output will be:

Deletion failed

Note that line one can only consist off number 1 to 20 and line two and three only off a string with 1 and 0.

This is what i have:

import java.util.Scanner;

public class A {
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        int level = sc.nextInt();
             if (level >= 1 && level <= 20) {
                System.out.println(level);
         }

        String reverse = "";
        String validering;

        String line1 = sc.next();

        String line2 = sc.next();

        // Reverse
        for(int i = line1.length() - 1; i >= 0; i--)
        {
            reverse = reverse + line1.charAt(i);
        }
            System.out.print(reverse);

        if (line1.equals(reverse)) {
            System.out.println("Deletion succeeded");
        }
        else if (!line1.equals(reverse)) {
            System.out.println("Deletion failed");
          }
      }
  }
Machavity
  • 30,841
  • 27
  • 92
  • 100
Cell
  • 3
  • 3
  • See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Klitos Kyriacou Jan 22 '20 at 09:33
  • You're not using line2 at all in your code. Why is that? – Scratte Jan 22 '20 at 11:09
  • Hmmm ... I thought I understood the requirements, but it turns out that I don't. Surely you only need to test if the all bits are switched (if N is odd) or no bits are switched if N is even? Why is the problem stated in such a complicated way? – Stephen C Jan 22 '20 at 12:44
  • @StephenC Maybe you do, and I just didn't. I don't understand **if each bit is switched times** – Scratte Jan 22 '20 at 12:47
  • The question needs to be stated more clearly. – Stephen C Jan 22 '20 at 12:49
  • What should `line3` be to result in a success if `line1 = 3` and `line2 = 001111`? `110000` or `111100`? – Scratte Jan 22 '20 at 12:55
  • maybe this has a better explanation: https://open.kattis.com/problems/erase or https://nordic.icpc.io/ncpc2013/ncpc2013problems.pdf – user85421 Jan 22 '20 at 13:04
  • @user85421 Thanks. There's no reversing strings. It's a bit-by-bit switch between 0 and 1. If the first line is even, there's obviously no switching. – Scratte Jan 22 '20 at 13:57

1 Answers1

0

According to Kattis' Erase Securely, you're not suppose to reverse the string. You're suppose to compare the bits one by one to see if the bits were flipped. So if String1 = "0" and N = 1, then String2 must be "1". If N = 2 (flip twice), then String2 must be "0". That's identical to String1.

So you can begin by seeing if N is even. If so, "Deletion succeeded" will apply if String1 is identical to String2.

If N is odd, then you need to loop over the Characters in the string and check if string1.charAt(i) is the same as string2.charAt(i). If they are the same then "Deletion failed".

The challenge on the site is to do it in less than a second. For that I'd probably try to load the values using Long.valueOf(64BitAtTime, 2) and use Exclusive OR, but I'm guessing that's not your aim.

Scratte
  • 3,056
  • 6
  • 19
  • 26