编程之美-3.1字符串移位包含问题

本文介绍了一种通过将源字符串复制拼接后使用strstr函数快速判断目标字符串是否能通过循环移位从源字符串中获得的方法,并提供了一种扩展算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:http://www.cnblogs.com/flyoung2008/archive/2012/03/03/2378749.html

假设字符串s1=AABCD,s2=CDAA,判断s2是否可以通过S1的循环移位得到字符串包含。
 如 s1移两位: 1.ABCDA->2.BCDAA 则此时包含了 S2="CDAA"


 解题思路:
 分解s1的循环移位得到:
 AABCD,ABCDA,BCDAA,CDAAB,.....
 如果我们将前面移走的字符串保留下来,则有:
 AABCD,AABCDA,AABCDAA,AABCDAAB,AABCDAABC,AABCDAABCD

这里,我们可以发现,实际对s1的循环移位得到的字符串实际为s1s1。
那么我们判断s2是否可以通过s1循环移位得到包含,则只需要判断s1s1中是否含有s2即可以。
用提高空间复杂度来换取时间复杂度的减低的目的。

 

     函数名: strstr
    包含文件:string.h   函数原型:extern char *strstr(char *str1, char *str2);
  功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。

  返回值:返回该位置的指针,如找不到,返回空指针。
其参数是传统的char   *型字符串,string型数据类型是不能作为其参数的。但可以通过string成员函数string::c_str()转换成char*类型。象这样调用: 
strstr(str1.c_str(),   str2.c_str())  

#include<iostream>
#include<string>

using namespace std;

bool rotstr(string src,string des)
{
    string tmp = src;
    src=src+tmp;
    if(strstr(src.c_str(),des.c_str())==NULL)
    {
        return false;
    
    }

    return true;

}

int main()
{
    string src="AABBCD";
    string des="DAAB";
    if(rotstr(src,des))
        cout<<"the string des is included in the rotated string of src "<<endl;
    else
        cout<<"the string des is not included in the rotated string of src "<<endl;
    return 0;
}


扩展:

找出s2中第一个字符在s1的位置记为p1,找出s2中最后一个字符在s1中可能的位置记为p2,计算所有的p1与p2间的差值,若等于s2的长度,则比较p1与p2间的字符串与s2,若两者相等则返回true,否则返回false

#include<iostream>
#include<string>
#include<vector>
using namespace std;

bool rotstr(string src,string des)
{
    //用vecPos1存储dest字符串第一个字符在src中可能出现的位置
    //用vecPos2存储dest字符串最后一个字符在src中可能出现的位置
    vector<int> vecPos1;
    vector<int> vecPos2;
    string str=src;
    int substrpos=str.find(des[0]);
    int pos=substrpos;
    //pos为字符在字符串中的位置
    while(substrpos!=-1)
    {
        cout<<pos<<endl;
        vecPos1.push_back(pos);
        str=src.substr(pos+1,src.length()-pos-1);
        substrpos=str.find(des[0]);
        pos=pos+1+substrpos;
    }
    str=src;
    substrpos=str.find(des[des.size()-1]);
    pos=substrpos;
    while(substrpos!=-1)
    {
        cout<<pos<<endl;
        vecPos2.push_back(pos);
        str=src.substr(pos+1,src.length()-pos-1);
        substrpos=str.find(des[des.size()-1]);
        pos=pos+1+substrpos;
    }
    int p1,p2;
    int len;
    string s;
    for(int i=0;i<vecPos1.size();i++)
    {
        for(int j=0;j<vecPos2.size();j++)
        {
            p1=vecPos1[i];
            p2=vecPos2[j];
            //最后一个字符出现在第一个字符前
            if((p2-p1)<0)
                len=src.size()-p1+p2+1;
            //最后一个字符出现在第一个字符后
            else
                len=p2-p1+1;
            if(len==des.size())
            {
                if(p2-p1>=0)
                    s=src.substr(p1,p2-p1+1);
                else
                    s=src.substr(p1,src.size()-p1)+src.substr(0,p2+1);
                if(s==des)
                {
                    return true;
                }

            }
        }
    }
    return false;
}

int main()
{
    string src="AABBCD";
    string des="DAAB";
    if(rotstr(src,des))
        cout<<"the string des is included in the rotated string of src "<<endl;
    else
        cout<<"the string des is not included in the rotated string of src "<<endl;
    int i;
    cin>>i;
    return 0;
}

PS:

检测两个字符串是否相等
对于C:strcmp(str1,str2)==0

对于C++: str1==str2
               原因:C++string类重载了==运算符以便检测字符串的内容是否相等

对于java:str1.equals(str2)
                说明:java中也有str1==str2 但是表达的含义不同
                java的对象变量与c++不同,java的对象变量可以看做C++的指针
                如:Date birth;      //java
                   等价于:Date * birth; // c++
               所以str1==str2 是判断两个指针所指向的地址是否相同,而不是指针所指向的内容是否相同
               所以java中检测两个字符串是否相等用:str1.equals(str2)或者:str1.compareTo(str2)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值