PAT A 1032 Sharing (25 分)

一、思路

遍历word1,并标记word1中每个字母为已访问;
遍历word2,若遇到已访问过的字母,则为公共后缀起点;

二、代码

#include 
#include 
using namespace std;
int main()
{
    vector<char> data(100000);
    vector<int> next(100000), visited(100000);
    int h1, h2, N, ans;
    scanf("%d %d %d", &h1, &h2, &N);
    for( int i = 0, addr; i < N; ++i )
    {
        scanf("%d ", &addr);
        scanf("%c %d", &data[addr], &next[addr]);
    }
    for( int i = h1; i != -1; i = next[i] )
        ++visited[i];
    for( ans = h2; ans != -1; ans = next[ans] )
        if( visited[ans] )
            break;
    if( ans == -1 )
        printf("-1");
    else printf("%05d", ans);
}

你可能感兴趣的:(PAT,A)