当前位置:网站首页>CareerCup它1.8 串移包括问题
CareerCup它1.8 串移包括问题
2022-07-05 20:48:00 【全栈程序员站长】
大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
【称号】
原文:
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”).
译文:
如果你有一个isSubstring函数,能够检測一个字符串是否是还有一个字符串的子串。 给出字符串s1和s2。仅仅使用一次isSubstring就能推断s2是否是s1的旋转字符串, 请写出代码。旋转字符串:”waterbottle”是”erbottlewat”的旋转字符串。
【分析】
我们也能够对循环移位之后的结果进行分析。 以S1 = ABCD为例,先分析对S1进行循环移位之后的结果,例如以下所看到的: ABCD—>BCDA—->CDAB—->DABC—->ABCD…… 如果我们把前面的移走的数据进行保留。会发现有例如以下的规律: ABCD—>ABCDA—->ABCDAB—->ABCDABC—->ABCDABCD…… 因此,能够看出对S1做循环移位所得到的字符串都将是字符串S1S1的子字符串。
假设S2能够由S1循环移位得到,那么S2一定在S1S1上,这样时间复杂度就减少了。
相同题目:编程之美之字符串移位包括问题
【代码一】
/*********************************
* 日期:2014-5-15
* 作者:SJF0115
* 题号: 字符串移位包括问题
* 来源: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;
}【代码二】
/*********************************
* 日期:2014-5-15
* 作者:SJF0115
* 题号: 字符串移位包括问题
* 来源: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;
}版权声明:本文博客原创文章,博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117665.html原文链接:https://javaforall.cn
边栏推荐
- Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- Cutting edge technology for cultivating robot education creativity
- Go file path operation
- Norgen AAV extractant box instructions (including features)
- Make Jar, Not War
- 教你自己训练的pytorch模型转caffe(一)
- Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
- leetcode:1139. 最大的以 1 为边界的正方形
- Nprogress plug-in progress bar
猜你喜欢

AI automatically generates annotation documents from code

小程序事件绑定

When steam education enters personalized information technology courses

Duchefa low melting point agarose PPC Chinese and English instructions

Classic implementation of the basic method of intelligent home of Internet of things

Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书

1. Strengthen learning basic knowledge points

Abnova丨CRISPR SpCas9 多克隆抗体方案

Use of thread pool

Applet global configuration
随机推荐
NPDP如何续证?操作指南来了!
台风来袭!建筑工地该如何防范台风!
渗透创客精神文化转化的创客教育
Abbkine BCA法 蛋白质定量试剂盒说明书
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
Open source SPL eliminates tens of thousands of database intermediate tables
How to form standard interface documents
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
Prosci LAG-3 recombinant protein specification
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
Abnova total RNA Purification Kit for cultured cells Chinese and English instructions
重上吹麻滩——段芝堂创始人翟立冬游记
Which is the best online collaboration product? Microsoft loop, notion, flowus
Informatics Olympiad 1340: [example 3-5] extended binary tree
Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
Duchefa MS medium contains vitamin instructions
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once