⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fig09_38.cpp

📁 经典书籍源代码啊。。。第三版。。。数据结构与算法分析——C++描述(第3版).
💻 CPP
字号:
// Runs the shortest path calculation from the adjacency map, returning a map
// that contains the "prev" entries for each word in the graph.
map<string,string> findChain( const map<string,vector<string> > & adjacentWords,
                              const string & first )
{
    map<string,string> previousWord;
    queue<string> q;

    q.push( first );
    while( !q.empty( ) )
    {
        string current = q.front( ); q.pop( );

        map<string,vector<string> >::const_iterator itr;
        itr = adjacentWords.find( current );

        const vector<string> & adj = itr->second;
        for( int i = 0; i < adj.size( ); i++ )
            if( previousWord[ adj[ i ] ] == "" )
            {
                previousWord[ adj[ i ] ] = current;
                q.push( adj[ i ] );
            }
    }
    previousWord[ first ] = "";

    return previousWord;
}

// After the shortest path calculation has run, computes the vector that
// contains the sequence of word changes to get from first to second.
vector<string> getChainFromPrevMap( const map<string,string> & previous,
                                    const string & second )
{
    vector<string> result;
    map<string,string> & prev = const_cast<map<string,string> &>( previous );

    for( string current = second; current != ""; current = prev[ current ] )
        result.push_back( current );

    reverse( result.begin( ), result.end( ) );

    return result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -