当前位置:网站首页>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
边栏推荐
- 王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
- 珍爱网微服务底层框架演进从开源组件封装到自研
- MySQL InnoDB架构原理
- Interpreting the daily application functions of cooperative robots
- [quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system
- 概率论机器学习的先验知识(上)
- Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
- wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
- Abnova 环孢素A单克隆抗体,及其研究工具
- Is it safe to open a stock account by mobile phone? My home is relatively remote. Is there a better way to open an account?
猜你喜欢
ProSci LAG-3 重组蛋白说明书
ProSci LAG3抗体的化学性质和应用说明
Duchefa丨MS培养基含维生素说明书
重上吹麻滩——段芝堂创始人翟立冬游记
Abnova DNA marker high quality control test program
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
教你自己训练的pytorch模型转caffe(三)
The development of research tourism practical education helps the development of cultural tourism industry
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
[record of question brushing] 1 Sum of two numbers
随机推荐
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
手机开户股票开户安全吗?我家比较偏远,有更好的开户途径么?
Abnova CRISPR spcas9 polyclonal antibody protocol
Popular science | does poor English affect the NPDP exam?
Usaco3.4 "broken Gong rock" band raucous rockers - DP
[Yugong series] go teaching course in July 2022 004 go code Notes
Propping of resources
Material Design组件 - 使用BottomSheet展现扩展内容(二)
ODPS 下一个map / reduce 准备
ClickHouse 复制粘贴多行sql语句报错
MySQL InnoDB架构原理
获取前一天的js(时间戳转换)
Écrire une interface basée sur flask
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
Simple understanding of interpolation search
Abnova丨血液总核酸纯化试剂盒预装相关说明书
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
How to renew NPDP? Here comes the operation guide!
Is it safe to open a stock account by mobile phone? My home is relatively remote. Is there a better way to open an account?
shell编程100例