2.7 Lab: Circular Shift
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");
}
}
}
相關文章
- [Javascript] Circular dependencyJavaScript
- Circular Spanning Tree
- 妙用ConstraintLayout的Circular positioningAI
- [LeetCode] 641. Design Circular DequeLeetCode
- JavaScript shift()JavaScript
- 2.7
- 【csapp lab】shell labAPP
- BZOJ3497 : Pa2009 Circular GameGAM
- 2.7(2)
- FSM:Enable shift register
- seed lab 2020 packet sniffing and spoofing lab
- CSAPP-Lab04 Architecture Lab 深入解析APP
- CSAPP-Lab03 Attack Lab 記錄APP
- Windows五次Shift漏洞Windows
- 2.7(學號:3025)
- 2.7 交易確認
- TypeScript 2.7 記錄TypeScript
- Error:Cannot build artifact xxx:war exploded‘ because it is included into a circular dependencyErrorUI
- [ABC150F] Xor Shift
- 精讀《ObjectEntries, Shift, Reverse...》Object
- Lab 1: MapReduce
- boom lab分析OOM
- CSAPP Malloc LabAPP
- CSAPP Cache LabAPP
- Wireshark Lab: HTTPHTTP
- sicp每日一題[2.7]每日一題
- cs144 lab0 lab1記錄CS144
- 4-bit shift register and down counter
- 2018-11-06 shift n.
- ChCore-lab3
- ChCore-lab0
- ChCore-lab1
- ChCore-lab2
- Lab 1 CMOS Inverter
- csapp Lab1APP
- Ubuntu 24.04 安裝 Python 2.7UbuntuPython
- 2.7 Overview of Oracle Resource Manager in a CDBViewOracle
- Kafka官方文件V2.7Kafka