You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dotnet-learn-vs/WebMVC/WebMVCApi/md/C++ 字符串比较的一下方法.md

1.0 KiB

icon date category headerDepth
edit 2023-01-31
cpp
5

C++ 字符串比较的一下方法

字符串比较是否包含

 bool canConstruct(std::string ransomNote, std::string magazine)
    {   //字符串等长情况
        int magazineLength =magazine.length();
        int ransomNoteLength =ransomNote.length();
        if(magazineLength == ransomNoteLength){
            for (size_t i = 0; i < ransomNoteLength; i++)
            {
                if(magazine[i]!=ransomNote[i]){
                   return false;
                }
            }
            return true;
        }

        if (magazineLength > ransomNoteLength)
        {
            int temp= 0;
             for (size_t i = 0; i < magazineLength; i++)
            { 
                if(magazine[i]==ransomNote[temp]){
                    temp++;
                }else{
                    temp = 0;
                }
                if(temp==ransomNoteLength){
                    return true;
                }
            }
        }
        return false;
    }