当前位置:网站首页>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
边栏推荐
- 小程序页面导航
- Duchefa cytokinin dihydrozeatin (DHZ) instructions
- How to make ERP inventory accounts of chemical enterprises more accurate
- Monorepo管理方法论和依赖安全
- 清除app data以及获取图标
- 台风来袭!建筑工地该如何防范台风!
- 2.8 basic knowledge of project management process
- 线程池的使用
- Norgen AAV extractant box instructions (including features)
- The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
猜你喜欢
Norgen AAV extractant box instructions (including features)
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
Abnova丨DNA 标记高质量控制测试方案
Graph embedding learning notes
Abbkine BCA法 蛋白质定量试剂盒说明书
Abnova丨血液总核酸纯化试剂盒预装相关说明书
实现浏览页面时校验用户是否已经完成登录的功能
Analysis of steam education mode under the integration of five Education
Typhoon is coming! How to prevent typhoons on construction sites!
如何让化工企业的ERP库存账目更准确
随机推荐
教你自己训练的pytorch模型转caffe(三)
E. Singhal and numbers (prime factor decomposition)
2022 Beijing eye health products exhibition, eye care products exhibition, China eye Expo held in November
Common view container class components
资源道具化
Abnova丨DNA 标记高质量控制测试方案
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?
手机开户股票开户安全吗?我家比较偏远,有更好的开户途径么?
研学旅游实践教育的开展助力文旅产业发展
Informatics Olympiad 1337: [example 3-2] word search tree | Luogu p5755 [noi2000] word search tree
Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
【刷题记录】1. 两数之和
获取前一天的js(时间戳转换)
The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
Composition of applet code
ProSci LAG-3 重组蛋白说明书
Use of form text box (II) input filtering (synthetic event)
如何让化工企业的ERP库存账目更准确
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
Cutting edge technology for cultivating robot education creativity