字符串旋转:
给定两字符串A和B,如果能将A从中间某个位置分割为左右两部分字符串(都不为空串),并将左边的字符串移动到右边字符串后面组成新的字符串可以变为字符串B时返回true。
例如:如果A=‘youzan’,B=‘zanyou’,A按‘you’‘zan’切割换位后得到‘zanyou’和B相同返回true。
2个不为空的字符串(说明:输入一个字符串以英文分号";"分割为2个字符串)
例如:youzan;zanyou 即为A=‘youzan’,B=‘zanyou’
输出true或false(表示是否能按要求匹配两个字符串)
youzan;zanyou
true
youzan;zyouan
false
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner s = new Scanner(System.in); String a = s.nextLine(); s.close(); String[] m = a.split(";"); if(m[0].length()!=m[1].length()){ System.out.print(false); return; } m[0] = m[0]+m[0]; System.out.print(m[0].contains(m[1])); } }把第一个字符串叠加起来问题会简单很多。
判断一个字符串旋转是否为另一个字符串,可以转换成等价问题,前面字符串拼接两份,再看是否包含第二个字符串,从而省去了旋转这些操作,而转换成字符串匹配问题,如果是笔试为了赶时间,可以直接调用系统的函数判断是否contain;平时刷题可以拿来练一下kmp算法;
以下是kmp算法的实现,已AC
import java.util.Scanner; import static java.lang.System.in; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(in); String[] data = sc.nextLine().split(";"); if (data[0] == null || data[1] == null || data[0].length() == 0 || data[1].length() == 0 || data[0].length() != data[1].length()) { System.out.println(false); return; } String str1 = data[0] + data[0], str2 = data[1]; int pos = kmp(str1.toCharArray(), str2.toCharArray()); System.out.println(pos == -1 ? false : true); } public static int kmp(char[] arr1, char[] arr2) { int i = 0, j = 0; int[] nextArr = getNextArr(arr2); while (i < arr1.length && j < arr2.length) { if (arr1[i] == arr2[j]) { i++; j++; } else if (nextArr[j] != -1) { j = nextArr[j]; } else { i++; } } return j == arr2.length ? i - j : -1; } public static int[] getNextArr(char[] arr) { if (arr.length == 1) { return new int[]{-1}; } int[] next = new int[arr.length]; next[0] = -1; next[1] = 0; int i = 2; int cn = 0; while (i < arr.length) { if (arr[i - 1] == arr[cn]) { next[i++] = ++cn; } else if (cn > 0) { cn = next[cn]; } else { next[i++] = 0; } } return next; } }
/* 我的想法是将字符串A循环移位,每移动一次,就和字符串B进行比较,知道与字符串相等或者字符串A移动结束 */ import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); String[] strarr = input.nextLine().split(";"); String str1 = strarr[0]; String str2 = strarr[1]; //将str1进行移位 for(int i = 0;i<str1.length();i++){ str1 = str1.substring(1)+String.valueOf(str1.charAt(0)); if(str1.equals(str2)){ System.out.println(true); return; } } System.out.println(false); } }
const readline = require('readline') const rl = readline.createInterface({ input: process.stdin, ouput: process.stdout }) let inArr = [] rl.on('line',line=>{ if(!line) return inArr.push(line.trim()) if(inArr.length === 1){ let s1 = inArr[0].split(';')[0] let s2 = inArr[0].split(';')[1] let flag = false for(let i in s1){ //stringObject.substr(start,length) if(s1.substr(i)+s1.substr(0,i) === s2){ flag =true break } } console.log(flag) } })
#include <iostream> #include <string> using namespace std; int main() { char c; bool flag=1; string str_a,str_b; while((c=cin.get()) !='\n') { if(c == ';') { flag = 0; continue; } if(flag == 1) str_a +=c; else str_b +=c; } int length = str_a.size(); char com = str_b[0]; string stra,straa; for(int i = 0;i<length;i++) { if(str_a[i] == str_b[0]) { stra = str_a.substr(0,i); straa = str_a.substr(i); straa +=stra; if(straa == str_b) { cout << "true" <<endl; break; } } } if(straa != str_b) cout << "false" << endl; return 0; }
#include <bits/stdc++.h>using namespace std;int main(){string s, s1, s2, s3, left, right;cin>>s;int m = s.find(';');s1 = s.substr(0, m);s2 = s.substr(m+1);int l = s1.length();bool flag = false;for(int i=0;i<=l/2;i++){left = s1.substr(0, i);right = s1.substr(i);s3 = right + left;if(s3 == s2){flag = true;break;}}if(flag)cout<<"true"<<endl;elsecout<<"false"<<endl;return 0;
#include<bits/stdc++.h> using namespace std; int main() { string s; cin >> s; string a = s.substr(0, s.find(';')); string b = s.substr(s.find(';')+1, string::npos); a+=a; if(a.find(b)!=string::npos&&a.size()==2*b.size()) cout<<"true"<<endl; else cout<<"false"<<endl; return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); //输入 String input=scanner.nextLine(); String[] inList=input.split(";"); String inStr1=inList[0]; String inStr2=inList[1]; //计算 boolean canOrNot=false; int l=inStr1.length(); for(int i=1;i<l;i++){ if(inStr2.equals(inStr1.substring(i,l)+inStr1.substring(0,i))){ canOrNot=true; break; } } //输出 System.out.println(canOrNot); } }
importjava.util.*;importjava.io.*;public class Main{public static void main(String[] args){Scanner sc = newScanner(System.in);String input = sc.nextLine();String[] inputs = input.split(";");String a = inputs[0];String b = inputs[1];booleanflag = false;for(inti = 1;i < a.length();i++){StringBuffer stringBuffer = newStringBuffer(a.substring(i,a.length()));stringBuffer.append(a.substring(0,i));if(stringBuffer.toString().equals(b)){flag = true;break;}}if(flag){System.out.println("true");}else{System.out.println("false");}}}
import java.io.*; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String[] str=br.readLine().split(";"); if(str[0].length()!=str[1].length()) { System.out.println(false); return; } StringBuffer sb=new StringBuffer(str[0]+str[0]); System.out.println(sb.toString().contains(str[1])); } }
import java.io.*; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String[] str=br.readLine().split(";"); int flag=0; int n=str[0].length(); if(n!=str[1].length()) { System.out.println(false); return; } while(true) { flag=str[0].indexOf(str[1].charAt(0),flag); if(flag==-1) { System.out.println(false); return; } else if(str[0].substring(flag).equals(str[1].substring(0,n-flag))) break; flag++; } if(str[0].subSequence(0, flag).equals(str[1].substring(n-flag))) System.out.println(true); else System.out.println(false); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.next(); String word1 = str.split(";")[0]; String word2 = str.split(";")[1]; if (word1.length() != word2.length()) { System.out.println("false"); } else { System.out.println((word1 + word1).contains(word2)); } } }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String input = sc.next(); String[] s = input.split(";"); String num1 = s[0]; String num2 = s[1]; if (num1.equals(num2)){ System.out.println("true"); } String temp = ""; boolean flag = false; for (int i = 1; i < num1.length(); i ++) { temp = num1.substring(i,num1.length()) + num1.substring(0, i); if (temp.equals(num2)) { System.out.println("true"); flag = true; } } if (!flag) { System.out.println("false"); } } } }
#include <stdio.h> #include <string.h> int main() { char str[100]; char str1[100], str2[100]; gets(str); int ix1, ix2; ix1 = ix2 = 0; int flag = 0; for (int i = 0; i < strlen(str); ++i) { if (str[i] == ';') { flag = 1; continue; } if (!flag) { str1[ix1++] = str[i]; } else { str2[ix2++] = str[i]; } } str1[ix1] = '\0'; str2[ix2] = '\0'; if (ix1 != ix2) { printf("false\n"); return 0; } for (int i = ix1; i < 2 * ix1; ++i) { str1[i] = str1[i - ix1]; } if (strstr(str1, str2) != NULL) { printf("true\n"); } else { printf("false\n"); } return 0; }
import sys mes = sys.stdin.readline().strip().split(';') A = mes[0] B = mes[1] flag = 0 for i in range(len(B)): if B[i] == A[0]: if B[i:] == A[0:len(B[i:])] and A[len(B[i:]):] == B[0:i]: flag = 1 break if flag: print('true') else: print('false')