HDU5487 difference of Languages (BFS)

Source: Internet
Author: User

Test instructions: Give you two automata, find the shortest (if there is the same shortest dictionary order of the smallest) can be received by one of the automata and can not be received by another automaton machine string.

A look is the automaton thought is divine question, later race finally had the thought.

Two automata state is less than 1000, so you can build a diagram, each node (U,V) represents the current state of the Automaton 1 and the state of the Automaton 2 V, and then the corresponding states receive [A-z] character will be transferred to the next state. Then from the origin (0,0) began wide search, found the first accpet[u]!=accept[v] is the state of the request. (When processing the time to give each automaton a state, used to indicate the mismatch when the situation, the state of all subsequent transfer to themselves, and not itself Accpet State, wide search can be)

#pragma warning (disable:4996) #include <iostream> #include <cstring> #include <cstdio> #include < algorithm> #include <vector> #include <cmath> #include <queue>using namespace std; #define MAXN 1005int N1, M1, K1;int n2, M2, k2;bool tar1[maxn];bool tar2[maxn];int go1[maxn][26];int go2[maxn][26];int prestate[maxn*m Axn];char prechar[maxn*maxn];bool vis[maxn*maxn];bool Check (int x) {return tar1[x/n2]! = tar2[x%n2];} int nextstate (int x, int c) {return go1[x/n2][c] * n2 + go2[x%n2][c];} int main () {int T; cin >> T; int ca = 0;while (t--) {memset (Go1,-1, sizeof (GO1)), memset (Go2,-1, sizeof (GO2)); Memset ( TAR1, 0, sizeof (TAR1)), memset (tar2, 0, sizeof (TAR2)), int accept;int UI, Vi;char ci[3];scanf ("%d%d%d", &n1, &AMP;M1, & AMP;K1); for (int i = 0; i < K1; ++i) {scanf ("%d", &accept); tar1[accept] = true;} for (int i = 0; I < M1; ++i) {scanf ("%d%d%s", &ui, &vi, CI); go1[ui][ci[0]-' a '] = VI;} for (int i = 0, I <= N1; ++i) {for (int k = 0; K &LT 26; ++k) {if (go1[i][k] = =-1) go1[i][k] = n1;}} scanf ("%d%d%d", &n2, &m2, &AMP;K2); for (int i = 0; i < K2; ++i) {scanf ("%d", &accept); tar2[accept] = true;} for (int i = 0; i < m2; ++i) {scanf ("%d%d%s", &ui, &vi, CI); go2[ui][ci[0]-' a '] = VI;} for (int i = 0, I <= n2; ++i) {for (int k = 0; k <; ++k) {if (go2[i][k] = = 1) go2[i][k] = n2;}} ++N1; ++n2;int ans = -1;memset (Vis, 0, sizeof (VIS));queue<int> Q; Q.push (0); Vis[0] = True;while (! Q.empty ()) {int state = Q.front (); Q.pop (); if (check (state)) {ans = state;break;} for (int k = 0; K <: ++k) {int nstate = Nextstate (state, k); if (!vis[nstate]) {Q.push (nstate); Vis[nstate] = True;prest Ate[nstate] = state;prechar[nstate] = k;}}} if ( -1 = = ans) {printf ("Case #%d:0\n", ++ca); continue;} String Ts;while (ans! = 0) {ts.push_back (char (' a ' + Prechar[ans])); ans = Prestate[ans];} Reverse (Ts.begin (), Ts.end ());p rintf ("Case #%d:%s\n", ++ca, Ts.c_str ());} return 0;}

HDU5487 difference of Languages (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.