当前位置:网站首页>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
边栏推荐
- Abnova丨 CD81单克隆抗体相关参数和应用
- Material design component - use bottomsheet to show extended content (II)
- Which is the best online collaboration product? Microsoft loop, notion, flowus
- 请查收.NET MAUI 的最新学习资源
- ts 之 泛型
- Duchefa丨低熔点琼脂糖 PPC中英文说明书
- ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
- poj 3414 Pots (bfs+线索)
- 产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
- Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
猜你喜欢
Duchefa丨P1001植物琼脂中英文说明书
leetcode:1755. 最接近目标值的子序列和
mysql全面解析json/数组
Which is the best online collaboration product? Microsoft loop, notion, flowus
Maker education infiltrating the transformation of maker spirit and culture
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
ClickHouse 复制粘贴多行sql语句报错
2.8 basic knowledge of project management process
教你自己训练的pytorch模型转caffe(二)
培养机器人教育创造力的前沿科技
随机推荐
leetcode:1755. 最接近目标值的子序列和
Usaco3.4 "broken Gong rock" band raucous rockers - DP
hdu2377Bus Pass(构建更复杂的图+spfa)
PHP deserialization +md5 collision
Simple understanding of interpolation search
AI 从代码中自动生成注释文档
shell编程100例
Binary search
3.3 project evaluation
研学旅游实践教育的开展助力文旅产业发展
重上吹麻滩——段芝堂创始人翟立冬游记
Écrire une interface basée sur flask
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
LeetCode: Distinct Subsequences [115]
CareerCup它1.8 串移包括问题
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
概率论机器学习的先验知识(上)
AI automatically generates annotation documents from code
Duchefa丨D5124 MD5A 培养基中英文说明书
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme