题目:

给定某字符串,判断该字符串中是否包含HelloWorld,出现HelloWorld不一定要连续,但顺序不变,如“HeByello,ByeWorByeld”就包含“HelloWorld”。

思路:

通过i,j来遍历两个字符串str1,str2(HelloWorld),假设长度分别为m,n;

当i>m或者j>n,则不包含;

当i<m且j==n-1,且str1[i]==str2[j],则包含;

当i<m且j<n,如果str1[i]==str2[j],i++,j++;否则,i++;

可以通过递归来实现,也可以通过循环来实现,详见代码。

代码:

#include<iostream>

using namespace std;

void IsHelloWorld(string word1,string word2,int i,int j,bool &flag){
int m=word1.size();
int n=word2.size(); if(i>=m || j>=n)
return; if(j==n-){
if(word1[i]==word2[j])
flag=true;
} else if(word1[i]==word2[j])
IsHelloWorld(word1,word2,i+,j+,flag); else
IsHelloWorld(word1,word2,i+,j,flag);
} bool IsHelloWorld_Recursive(string word1,string word2){
int m=word1.size();
int n=word2.size();
if(m<n)
return false; bool flag=false;
IsHelloWorld(word1,word2,,,flag); return flag;
} bool IsHelloWorld(string word1,string word2){
int m=word1.size();
int n=word2.size(); int i=;int j=;
while(i<m && j<n){
if(j==n-){
if(word1[i]==word2[j])
return true;
}
else if(word1[i]==word2[j]){
i++;
j++;
}
else
i++;
}
return false;
} int main(){
string word1="HeMylloHelloNameWorIsldHAHA";
string word2="HelloWorld"; cout<< IsHelloWorld_Recursive(word1,word2) <<endl;
cout<< IsHelloWorld(word1,word2) <<endl; return ;
}

(算法)判断字符串中是否包含HelloWorld的更多相关文章

  1. PHP判断字符串中是否包含指定字符串,支持中文哦

    RT,随手写的 /** * 判断字符串中是否包含指定字符串 * @var source 源字符串 * @var target 要判断的是否包含的字符串 * @return bool */ functi ...

  2. [C#]判断字符串中是否包含中文

    关键代码: /// <summary> /// 判断字符串中是否包含中文 /// </summary> /// <param name="str"&g ...

  3. java判断字符串中是否包含中文 过滤中文

    package com.test; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test ...

  4. java 判断字符串中是否包含中文并过滤掉中文

      java判断字符串中是否包含中文并过滤掉中文 CreateTime--2017年9月6日08:48:59 Author:Marydon 1.判断字符串中是否包含中文方法封装 /** * 判断字符串 ...

  5. 判断字符串中是否包含Emoji表情代码

    判断字符串中是否包含Emoji表情代码: + (BOOL)stringContainsEmoji:(NSString *)string { __block BOOL returnValue = NO; ...

  6. python判断字符串中是否包含子字符串

    python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1:     print('存在') else:     print('不存在' ...

  7. php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos

    php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...

  8. Node.js之判断字符串中是否包含某个字符串

    server.txt内容如下: 阿里云服务器 关于应用场景,就不多说了,字符串是不论是后端开发还是前端开发等,都是要经常打交道了. test.js(node.js代码,只要被本地装了node.js环境 ...

  9. ***用php的strpos() 函数判断字符串中是否包含某字符串的方法

    判断某字符串中是否包含某字符串的方法 if(strpos('www.idc-gz.com','idc-gz') !== false){ echo '包含'; }else{ echo '不包含'; } ...

随机推荐

  1. Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈

    C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...

  2. Uva 12889 One-Two-Three

      Your little brother has just learnt to write one, two and three, in English. He has written a lot ...

  3. tomcat部署应用仅需ip和port访问

    一.使用ip和port访问应用项目: 打开tomcat安装根目录,打开conf目录下server.xml,找到<Host>节点,并且在该节点下新增: <Context   docBa ...

  4. 小米路由通过SSH添加静态路由表之后无法跳转的问题

    1.确定系统已经开启了转发功能: /etc/sysctl.conf下的配置项目为net.ipv4.ip_forward = 1 2.关闭防火墙的REJECT,也就是修改/etc/config/fire ...

  5. cat ,more ,less 命令的使用和差别

    cat命令功能用于显示整个文件的内容单独使用没有翻页功能因此常常和more命令搭配使用,cat命令还有就是将数个文件合并成一个文件的功能. more命令功能:让画面在显示满一页时暂停,此时可按空格健继 ...

  6. Visual Studio 2013 密钥

    Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥) ...

  7. MySql 数据库导入"Unknown command '\n'."错误解决办法

    原文地址:http://www.cnblogs.com/bingxueme/archive/2012/05/15/2501999.html 在CMD 下 输入: Mysql -u root -p -- ...

  8. Sublime Text3 配置 Python2 Python3

    { "cmd": "C:/Python27/python.exe", "-u", "$file"], "fil ...

  9. UCOS移植心得(

    移植UCOS之前,你首先应该做好三件事: 1.弄懂UCOS,这是谁都知道的哦 ^_^ 2. 弄懂你想要移植到的硬件平台 3. 清楚你使用的编译器是如何处理函数的局部变量和怎么样处理函数间的参数传递 这 ...

  10. android 内存溢出oom错误的一些小见解

    转:http://blog.csdn.net/xuhui_7810/article/details/9493681 我们在代码里调用setBackgroundResource(int resid)来设 ...