manacher,Sundy,morris算法 C++实现


class Solution
{
public:
    /***************************************************
    *  函数功能:寻找最长回文子串 manacher算法
    *  参数说明
    *       输入参数:
    *       输出参数:
    *  复杂性分析:时间复杂度:O(n),空间复杂度:O(n)
    *  题目来源  :
    *  日期:2018-07-25-16.23
    ***************************************************/
    int manacher(string str)
    {
       if(str.size()<1) return 0;
       string new_str="";
       manacher(str,new_str);
       int length=new_str.size();
       int* pA=new int[length]();
       int index=-1;
       int pR=-1;
       int max_length=-1;
       for(int i=0;i<length;i++)
       {
           pA[i]=pR>i?min(pA[2*index-i],pR-i):1;
           while(i-pA[i]>-1 && i+pA[i]<length)
           {
               if(new_str[i-pA[i]]!=new_str[i+pA[i]])
               {
                   break;
               }
               pA[i]++;
           }
           if(i+pA[i]>pR)
           {
               pR=i+pA[i];
               index=i;
           }
           max_length=max(max_length,pA[i]);
       }

       delete[] pA;
       return max_length-1;
    }

private:
    void manacher(string str,string& new_str)
    {
        for(int i=0;i<str.size();i++)
        {
            new_str.push_back('#');
            new_str.push_back(str[i]);
        }
        new_str.push_back('#');
    }
};

class Solution2
{
public:
    /***************************************************
    *  函数功能:利用Mnacher算法解决回文字符串问题
    *  参数说明
    *       输入参数:
    *       输出参数:
    *  复杂性分析:时间复杂度:O(n),空间复杂度:O()
    *  题目来源  :
    *  日期:2018-07-25-11.26
    ***************************************************/
    string shortestEnd(string str)
    {
        if(str.size()<1) return "";
        string new_str;
        shortestEnd(str,new_str);
        int length=new_str.size();
        int* pA=new int[length]();
        int index=-1;
        int pR=-1;
        int lastR=-1;
        for(int i=0;i<length;i++)
        {
            pA[i]=pR>i?min(pA[2*index-i],pR-i):1;
            while(i-pA[i]>-1 && i+pA[i]<length)
            {
                if(new_str[i-pA[i]]!=new_str[i+pA[i]])
                {
                    break;
                }
                pA[i]++;
            }
            if(i+pA[i]>pR)
            {
                pR=i+pA[i];
                index=i;
            }
            if(pR==length)
            {
                lastR=i;
                break;
            }
        }
        string result="";
        int re_len=2*lastR-length;
        for(int i=re_len;i>0;i-=2)
        {
            result.push_back(new_str[i]);
        }

        delete[] pA;

        return result;
    }
private:
    void shortestEnd(string str,string& new_str)
    {
        for(int i=0;i<str.size();i++)
        {
            new_str.push_back('#');
            new_str.push_back(str[i]);
        }
        new_str.push_back('#');
    }
};

class Solution3
{
public:
    /***************************************************
    *  函数功能:Sundy算法解决字符串匹配问题
    *  参数说明
    *       输入参数:
    *       输出参数:
    *  复杂性分析:时间复杂度:O(),空间复杂度:O()
    *  题目来源  :
    *  日期:2018-07-25-11.26
    ***************************************************/
    int getIndexOf(string str,string match)
    {
        if(str.size()<1 || match.size()<1 || str.size()<match.size()) return -1;
        int R=match.size();
        int pos=0;
        for(int i=0;i<str.size();i++)
        {
            if(str[i]!=match[pos])
            {

                while(pos>0 && str[R]!=match[pos])
                {
                    pos--;
                }
                i=R-pos;
                pos=0;
                R=i+match.size();
            }else
            {
                if(pos==match.size()-1)
                {
                    return i-pos;
                }
                pos++;
            }
        }
        return -1;
    }
};

struct TreeNode
{
    int val;
    TreeNode *left,*right;
    TreeNode(int x):val(x),left(NULL),right(NULL){}
};

class Solution4
{
public:
    /***************************************************
    *  函数功能:morris 先序遍历
    *  参数说明
    *       输入参数:
    *       输出参数:
    *  复杂性分析:时间复杂度:O(n),空间复杂度:O(1)
    *  题目来源  :
    *  日期:2018-07-25-14.47
    ***************************************************/
    void morrisPre(TreeNode* root)
    {
        if(root==NULL) return ;
        TreeNode* curr=root;
        TreeNode* mostRight=NULL;
        while(curr!=NULL)
        {
            mostRight=curr->left;

            if(mostRight!=NULL)
            {
                while(mostRight->right!=NULL && mostRight->right!=curr)
                {
                    mostRight=mostRight->right;
                }
                if(mostRight->right==NULL)
                {
                    mostRight->right=curr;
                    cout<<curr->val<<" ";
                    curr=curr->left;

                    continue;
                }else
                {
                    mostRight->right=NULL;
                }
            }else
            {
                cout<<curr->val<<" ";
            }
            curr=curr->right;
        }
    }

    /***************************************************
    *  函数功能:morris 中序遍历
    *  参数说明
    *       输入参数:
    *       输出参数:
    *  复杂性分析:时间复杂度:O(n),空间复杂度:O(1)
    *  题目来源  :
    *  日期:2018-07-25-14.47
    ***************************************************/
    void morrisMid(TreeNode* root)
    {
        if(root==NULL) return ;
        TreeNode* curr=root;
        TreeNode* mostRight=NULL;
        while(curr!=NULL)
        {
            mostRight=curr->left;

            if(mostRight!=NULL)
            {
                while(mostRight->right!=NULL && mostRight->right!=curr)
                {
                    mostRight=mostRight->right;
                }
                if(mostRight->right==NULL)
                {
                    mostRight->right=curr;
                    curr=curr->left;
                    continue;
                }else
                {
                    mostRight->right=NULL;
                }
            }

            cout<<curr->val<<" ";

            curr=curr->right;
        }
    }

    void digui_Mid(TreeNode* root)
    {
        if(root==NULL)
        {
            return ;
        }
        digui_Mid(root->left);
        cout<<root->val<<" ";
        digui_Mid(root->right);
    }
};

猜你喜欢

转载自blog.csdn.net/csu_guo_liang/article/details/81213389