当前位置:网站首页>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
边栏推荐
- Redis唯一ID生成器的实现
- [record of question brushing] 1 Sum of two numbers
- Norgen AAV提取剂盒说明书(含特色)
- 启牛2980有没有用?开户安全吗、
- 中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
- [Yugong series] go teaching course in July 2022 004 go code Notes
- 【UE4】UnrealInsight获取真机性能测试报告
- 2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
- Abnova DNA marker high quality control test program
- Abnova blood total nucleic acid purification kit pre installed relevant instructions
猜你喜欢
Abnova丨血液总核酸纯化试剂盒预装相关说明书
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
PHP deserialization +md5 collision
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
The development of research tourism practical education helps the development of cultural tourism industry
Applet event binding
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
PHP反序列化+MD5碰撞
IC popular science article: those things about Eco
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
随机推荐
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?
Implementation of redis unique ID generator
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
科普|英语不好对NPDP考试有影响吗 ?
表单文本框的使用(二) 输入过滤(合成事件)
当Steam教育进入个性化信息技术课程
Abnova丨血液总核酸纯化试剂盒预装相关说明书
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
Duchefa丨MS培养基含维生素说明书
How to open an account online for futures? Is it safe?
Monorepo管理方法论和依赖安全
Web Service简单入门示例
Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
MySQL InnoDB架构原理
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
解读协作型机器人的日常应用功能
leetcode:1755. 最接近目标值的子序列和
The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
Make Jar, Not War