zoukankan      html  css  js  c++  java
  • 编程之美——3.1字符串移位包含问题

    假设字符串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即可以。
    用提高空间复杂度来换取时间复杂度的减低的目的。

    遇到的问题:

    : error C2664: 'strcmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    出现错误的原因为

    strstr函数原型为: 
    extern char *strstr(char *str1, char *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;
    }


    附:

    检测两个字符串是否相等
    对于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)



    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
    分享到: 更多
  • 相关阅读:
    NgModelController: $setViewValue,$render,Formatter, Parser
    #!/usr/bin/env python与#!/usr/bin/python的区别
    post发送数据 mypost input 改变事件
    post发送 ArrayBuffer
    C# 字符串到字节数组,字节数组转整型
    C# WebKitBrowser 设置内容
    C# Tuple 创建一个新二元集合
    C# 时间对比
    C# 控件调整
    C# invoke和begininvoke的用法 __委托
  • 原文地址:https://www.cnblogs.com/flyoung2008/p/2378749.html
Copyright © 2011-2022 走看看