Compare and print

若如初见. 提交于 2020-01-25 06:43:09

问题


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");
          }
      }
  }

回答1:


According to https://open.kattis.com/problems/erase, 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.



来源:https://stackoverflow.com/questions/59856384/compare-and-print

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!