2.7 Lab: Circular Shift

RookieSheldon發表於2020-12-18

2.7 Lab: Circular Shift
This lab is an introductory lab to make sure you know how to submit your code. Note that you are welcome to use your own IDE and copy your code into the labs, or to use the code editor that is provided.

A string s is a circular shift (also known as a circular rotation) of a string t if it matches when the characters are circularly shifted by some number of positions; e.g. “ACTGACG” is a circular shift of “TGACGAC” and vice versa. Detecting this condition is important in the study of genomic sequences. Write a Java program that checks if the two command line arguments it is given are circular shifts of one another. Note that the test can be done in one line with a clever use of the built in String methods. Your program should output Yes if they are circular shifts and No if they are not.

import java.util.ArrayList;
        import java.util.List;
        import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String s = cin.next();
        String t = cin.next();
        char[] a = s.toCharArray();
        char[] b = t.toCharArray();
        int count = 0;
        int count1 = 0;
        int count2 = 0;
        List<Integer> index1 = new ArrayList<Integer>();
        if(a.length == b.length){
            for(char c :b){
                if(a[0]==c){
                    for(int n=0;n<b.length;n++){
                        if(a[0]==b[n]){
                            index1.add(n);
                        }
                    }
                    for(int n1 =0; n1<index1.size(); n1++){
                        count1++;
                        int k = index1.get(n1);
                        char[]array  = new char[a.length];
                        for(int n2 = 0; n2<a.length;n2++) {
                            if (k == a.length-1 ) {
                                if(n2 ==a.length-1){
                                    array[n2]=a[0];
                                }
                                else{
                                    array[n2] = a[n2+1];
                                }

                            }
                            else if(k ==1){
                                if(n2 ==0){
                                    array[0]=a[a.length-1];
                                }
                                else{
                                    array[n2] = a[n2-1];
                                }
                            }
                            else {
                                if (k + n2 > a.length) {
                                    array[n2] = a[n2 + k - a.length];
                                } else if (k + n2 == a.length) {
                                    array[n2] = a[0];
                                } else
                                    array[n2] = a[n2 + k];
                            }
                        }
                        for(int n3 = 0; n3<array.length;n3++){
                            if(!(array[n3]==b[n3])){
                                break;
                            }
                            else
                                count2++;
                        }
                        if(count2 == array.length){
                            System.out.println("Yes");
                            count1=-1;
                            break;
                        }
                    }
                    if(count1 == index1.size()){
                        System.out.println("No");
                    }

                }
                else count++;
                if(count == b.length){
                    System.out.println("No");
                }
            }
        }else{
            System.out.println("No");
        }

    }
}