洛谷p1032字串變換c++

原題

主要運用的是子字符串的替換和hash

關於子字符串的替換:一開始我用的是數組,2000多字,寫的頭大。正解應該用string和replace函數,非常方便!

hash:找一個合適的hash函數,然後構建hash表即可。

下方的代碼可以在tyvj上ac。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
string a,b,usea[7],useb[7],st[10007];int cnt=1,hcnt=0;
int heade[10007],nexte[10007];
struct node{
    string s;
    int step;
};
queue<node> q;
int hashment(string s)
{
    int k=0;
    for(int i=0;i<=s.size();++i)
    {
        k=abs((k*33+s[i])%10007);
    }
    int n=heade[k];
    while(n!=0)
    {
        if(st[n]==s) return 1;
        else n=nexte[n];
    }
    nexte[++hcnt]=heade[k];
    heade[k]=hcnt;
    st[hcnt]=s;
    return 0;
}
int main()
{
    cin>>a;
    cin>>b;
    while(cin>>usea[cnt]>>useb[cnt])
    {
        cnt++;
    }
    q.push((node){a,0});
    while(q.size()>0)
    {
        node t=q.front();q.pop();
        if(t.s==b)
        {
            cout<<t.step;
            return 0;
        }
        if(t.step==10) continue;
        for(int i=1;i<=cnt;++i)
        {
            for(int j=0;j<=t.s.size()-1;++j)
            {
                if(j+usea[i].size()>t.s.size())
                break;
                if(t.s[j]==usea[i][0])
                {
                    bool ok=1;
                    for(int k=1;k<=usea[i].size()-1;++k)
                    {
                        if(t.s[j+k]!=usea[i][k])
                        {
                            ok=0;
                            break;
                        }
                    }
                    if(ok)
                    {
                        string k1=t.s;
                        k1=k1.replace(j,usea[i].size(),useb[i],0,useb[i].size());
                        if(hashment(k1)==0)
                        {
                            q.push((node){k1,t.step+1});
                        }
                    }
                }
            }
        }
    }
    cout<<"NO ANSWER!";
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章