当前位置:网站首页>Careercup its 1.8 serial shift includes problems
Careercup its 1.8 serial shift includes problems
2022-07-05 20:51:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I've prepared for you today Idea Registration code .
【 The title of 】
original text :
1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring ( i.e., “waterbottle” is a rotation of “erbottlewat”).
translation :
If you have one isSubstring function , It can detect whether a string is a substring of another string . Give the string s1 and s2. Just use it once isSubstring Can infer s2 Whether it is s1 Rotation string for , Please write the code . Rotate string :”waterbottle” yes ”erbottlewat” Rotation string for .
【 analysis 】
We can also analyze the results after cyclic shift . With S1 = ABCD For example , First analyze the right S1 The result of cyclic shift , As you can see below : ABCD—>BCDA—->CDAB—->DABC—->ABCD…… If we keep the previously removed data . You will find, for example, the following rules : ABCD—>ABCDA—->ABCDAB—->ABCDABC—->ABCDABCD…… therefore , Can see right S1 The string obtained by cyclic shift will be a string S1S1 Substring of .
hypothesis S2 Can be made by S1 Cyclic shift results in , that S2 It must be S1S1 On , In this way, the time complexity is reduced .
Same topic : The beauty of programming string shifting includes problems
【 Code 1 】
/*********************************
* date :2014-5-15
* author :SJF0115
* Question no : String shifting includes problems
* source :CareerCup
**********************************/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
bool isSubstring(char* str1,char* str2){
if(str1 == NULL || str2 == NULL){
return false;
}
if(strstr(str1,str2) != 0){
return true;
}
return false;
}
bool IsRotate(char* str1,char* str2){
int i,j;
if(str1 == NULL || str2 == NULL){
return false;
}
int len1 = strlen(str1);
char* str3 = new char(len1*2+1);
strcpy(str3,str1);
strcat(str3,str1);
//str3 = str1+str1
if(isSubstring(str3,str2)){
return true;
}
return false;
}
int main(){
char str1[6] = "AABCD";
char str2[5] = "CDAA";
bool result = IsRotate(str1,str2);
cout<<result<<endl;
return 0;
}【 Code 2 】
/*********************************
* date :2014-5-15
* author :SJF0115
* Question no : String shifting includes problems
* source :CareerCup
**********************************/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
bool isSubstring(string str1,string str2){
if(str1.find(str2) != string::npos) {
return true;
}
return false;
}
bool IsRotate(string str1,string str2){
string str3 = str1+str1;
if(isSubstring(str3,str2)){
return true;
}
return false;
}
int main(){
string str1 = "apple";
string str2 = "pleap";
bool result = IsRotate(str1,str2);
cout<<result<<endl;
return 0;
}Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117665.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢

How to make ERP inventory accounts of chemical enterprises more accurate

Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved

Duchefa d5124 md5a medium Chinese and English instructions

Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing

Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral

Chemical properties and application instructions of prosci Lag3 antibody

Abnova丨CRISPR SpCas9 多克隆抗体方案

Make Jar, Not War

Duchefa low melting point agarose PPC Chinese and English instructions

Écrire une interface basée sur flask
随机推荐
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
ClickHouse 复制粘贴多行sql语句报错
How to open an account online for futures? Is it safe?
ODPS 下一个map / reduce 准备
MySQL InnoDB架构原理
Monorepo管理方法论和依赖安全
Duchefa p1001 plant agar Chinese and English instructions
Duchefa丨低熔点琼脂糖 PPC中英文说明书
How to renew NPDP? Here comes the operation guide!
Chemical properties and application instructions of prosci Lag3 antibody
Applet event binding
Analyze the knowledge transfer and sharing spirit of maker Education
Applet global configuration
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
poj 3414 Pots (bfs+线索)
Applet project structure
Binary search
Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
实现浏览页面时校验用户是否已经完成登录的功能