当前位置:网站首页>【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
2022-07-02 06:12:00 【谢西山】
写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
例如:给定s1 =AABCD和s2 = BCDAA,返回1
给定s1=abcd和s2=ACBD,返回0.
AABCD左旋一个字符得到ABCDA
AABCD左旋两个字符得到BCDAA
AABCD右旋一个字符得到DAABC
方案一
分析:s1旋转一次为ABCDA,和s2比较,调用strcmp,返回值等于0 的值,则说明是s2是s1旋转得到的,否则继续在ABCDA的基础上再旋转一次,得到BCDAA,和s2比较,调用strcmp,返回值等于0 的值,则说明是s2是s1旋转得到的,继续循环,直到循环5次,或着在循环中碰到正好s1=s2,那么说明,s2是s1旋转得到的。
# define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
int judge(char* s1, char* s2,int len)
{
//先旋转一个字符
for (int i = 1; i <= len; ++i)
{
//i为1时第一次旋转
char tem = *s1;
int j=0;
for (j = 0; j < len; ++j)
{
*(s1 + j) = *(s1 + j + 1);
}
*(s1+j) = tem;//将刚才取出的值放到最后一个元素
if (strcmp(s1, s2) == 0)
{
return 1;
}
}
}
int main()
{
char s1[] = "AABCD";
char s2[] = "BCDAA";
int len = strlen(s1);
if (judge(s1,s2,len))
printf("Fing it");
else
printf("No find");
return 0;
}
方案2
使用函数strcat组成新的字符串s3,AABCDAABCD,用s2来遍历比较函数是否为s1的子函数,若是则s2是s1旋转得到的。
strncat 函数
Append characters of a string.
char *strncat(char *strDest,constchar *strSource,size_tcount);
# define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
int FlipCheck(char* arr1, char* arr2)
{
if (strlen(arr1) != strlen(arr2))//如果两个字符串长度不相等,那么不用比较都直到不是旋转所得。
return 0;
strncat(arr1, arr1, strlen(arr1));//append a string
return strstr(arr1, arr2) != NULL;//查找子字符串,找到则返回值为非0
}
int main()
{
char arr1[20] = "AABCD";
char arr2[10] = "BCDAA";
if (FlipCheck(arr1, arr2))
printf("Find it");
else
printf("NO Find");
return 0;
}
我发了讲解视频,在评论区,自己去看
今日垃圾话:那个公司又叫我了,啊啊啊
边栏推荐
- Cookie plugin and localforce offline storage plugin
- Detailed explanation of BGP message
- Shenji Bailian 3.54-dichotomy of dyeing judgment
- Pbootcms collection and warehousing tutorial quick collection release
- Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
- The official zero foundation introduction jetpack compose Chinese course is coming!
- 一起学习SQL中各种join以及它们的区别
- 锐捷EBGP 配置案例
- WLAN相关知识点总结
- Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk
猜你喜欢

找到页面当前元素z-index最高的数值

神机百炼3.52-Prim

数据科学【九】:SVD(二)

数据回放伴侣Rviz+plotjuggler

Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!

Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4

LeetCode 78. subset

New version of dedecms collection and release plug-in tutorial tool

LeetCode 78. 子集

WLAN相关知识点总结
随机推荐
找到页面当前元素z-index最高的数值
网络相关知识(硬件工程师)
BGP报文详细解释
LeetCode 77. combination
Cglib代理-代码增强测试
IPv6 experiment and summary
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
LeetCode 47. Full arrangement II
队列(线性结构)
深入学习JVM底层(三):垃圾回收器与内存分配策略
Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
VRRP之监视上行链路
I/o multiplexing & event driven yyds dry inventory
Scheme and implementation of automatic renewal of token expiration
Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
Classic literature reading -- deformable Detr
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
数据科学【八】:SVD(一)
Codeforces Round #797 (Div. 3) A—E
Support new and old imperial CMS collection and warehousing tutorials