编程之美3.1 字符串移位包含问题
来源:互联网 发布:c语言创建一张表的语句 编辑:程序博客网 时间:2024/06/06 19:08
1. 简述
给定两个字符串s1和s2,要求判断s2是否能够被s1做循环移位得到的字符串包含。例如,给定s1=AABCD和s2=CDAA,返回true;给定s1=ABCD,s2=ACBD,返回false。
2. 思路
将问题转化为:判断s1s1中是否包含s2子串。即对于s1=AABCD和s2=CDAA,判断AABCDAABCD中是否存在AA。
使用KMP匹配就行,对于s1s1也不用特地去开辟空间,假设s1的字符串指针为str,长度为len_str,只需len_str += len_str和str[i%len_str]即可。
3. 参考资料
编程之美,3.1节,字符串移位包含的问题
自己的方法如下:
#include <stdio.h>
#include <string.h>
char src[] = "AABCD";
char des[] = "CDAA";
int Is_Include(char src[], char des[]);
int main()
{
printf("the string src = %s \n", src);
printf("the string des = %s \n", des);
int src_len = 0;
int des_len = 0;
int result = 0;
src_len = strlen(src);
des_len = strlen(des);
if ( src_len < des_len )
{
result = Is_Include(src, des);
}
else
{
result = Is_Include(des, src);
}
if(result)
{
printf("the string src includes the string des!\n");
}
else
{
printf("the string src does not include the string des!\n");
}
return 0;
}
int Is_Include(char src[], char des[])
{
int i = 0;
int j = 0;
int des_len = strlen(des);
int src_len = strlen(src);
for (i = 0; des[i] != '\0'; i++)
{
if( src[0] != des[i])
{
continue;
}
else
{
for(j = 1; j < src_len; j++)
{
if(src[j] != des[ (i + j + des_len) % des_len])
{
break;
}
}
if (j == src_len)
{
return 1;
}
}
}
return 0;
}
- 编程之美---->字符串移位包含问题
- 《编程之美3.1:字符串移位包含问题》
- 《编程之美》3.1 字符串移位包含的问题
- 编程之美-3.1字符串移位包含问题
- 编程之美3.1 字符串移位包含问题
- 字符串移位包含问题(编程之美3.1)
- [编程之美3.1]字符串移位包含的问题
- 编程之美3.1 字符串移位包含问题
- 编程之美3.1 字符串移位包含问题
- 编程之美3.1字符串移位包含问题
- 编程之美——3.1字符串移位包含问题
- 编程之美--3.1字符串移位包含的问题
- 编程之美3.1 字符串移位包含的问题
- 3.1- 编程之美-字符串移位包含的问题
- 编程之美------3.1 字符串移位包含的问题
- 编程之美--3.1字符串移位包含问题
- 编程之美3.1 字符串移位包含问题
- 编程之美3.1 字符串移位包含问题
- Qt实现全局热键
- I2C总线协议学习笔记http://hi.baidu.com/312860519/item/a72f9eeb644404275a2d6492
- nokia c503
- 友善之臂Micro2440下的Embedded Linux的硬件时间设置
- FastDFS的配置、部署与API使用解读——设置FastDFS配置参数的两种方式
- 编程之美3.1 字符串移位包含问题
- hdu(4006)优先队列求第K大的数
- android面试题总结加强再加强版(一)
- MyISAM 和 InnoDB 讲解
- SAP作业类型
- 用TextView显示带图片的效果
- 星巴克为移动支付带来了革命性的变化
- android webview里设置cookie
- Whatser破产给创业公司的7个教训