The first line is a string (lowercase letter) that indicates the type of the character that appears.
The second line is the constraint relationship. a1 b1 a2 b2 a3 b3...... ai bi indicates that ai must be in front of bi.
Output all sequences that meet the constraints in Lexicographic Order
For example:
Y z x
X z
Xyz
Xzy
Yxz
Solution: the question must be output in Lexicographic Order. Sort the first line of strings first.
All constraints change to edges of a directed graph. x z indicates the edges from vertex x to vertex z.
Whether the sequence conflicts with the directed graph. If the sequence does not conflict with the sequence, the sequence is correct. Otherwise, the sequence is removed.
Use deep search to search all satisfied conditions
Each time a vertex with an input degree of 0 is found, it begins to enter the next layer, store it to a [len], and subtract 1 from the inbound degree of the vertex connected to this vertex.
Until the number of searched layers len equals the length of the string, a [] is output.
That is, the idea of integrating topological sorting in the search process until there is no vertex with an inbound degree of 0.
Pruning: converting a string to a number can greatly improve the efficiency, for example, converting y z x to 2 3 1
Code:
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <stdlib. h>
# Include <algorithm>
Using namespace std;
# Deprecision MAX 200
Char comment [MAX], ch2 [MAX], ch3 [MAX];
Int n, m, zm [MAX], edge [MAX] [MAX], to [MAX], visit [MAX], a [MAX];
Bool comp (char a, char B)
{
Return a <B;
}
Void DFS (int len)
{
Int I, j;
If (len = n) // if the number of layers equals the string length, the output is
{
For (I = 0; I <n; I ++)
{
Printf ("% c", outputs [a [I]);
}
Printf ("\ n ");
Return;
}
For (I = 0; I <n; I ++)
{
If (! Visit [I] &! To [I])
{
For (j = 0; j <n; j ++) // subtract 1 from the entry of the vertex connected to this vertex.
{
If (edge [I] [j] = 1)
{
To [j] --;
}
}
Visit [I] = 1; // mark this point for access
A [len] = I;
DFS (len + 1 );
For (j = 0; j <n; j ++) // restore the site: add the reduced inbound traffic back.
{
If (edge [I] [j] = 1)
{
To [j] ++;
}
}
Visit [I] = 0; // restore the site
}
}
Return;
}
Int main ()
{
Int I, k, pd = 0;
While (gets (ch3 ))
{
If (pd = 0)
Pd = 1;
Else
Printf ("\ n"); // empty lines are output between test cases
Gets (ch2 );
Memset (zm,-1, sizeof (zm ));
Memset (edge,-1, sizeof (edge ));
Memset (to, 0, sizeof ());
Memset (visit, 0, sizeof (visit ));
For (I = 0, n = 0; ch3 [I]! = '\ 0'; I ++) // remove Spaces
{
If (ch3 [I] = '')
Continue;
Pipeline [n ++] = ch3 [I];
}
Sort (sort, sort + n, comp); // sort
For (I = 0; I <n; I ++) // process string 1 and convert the letter to an edge from 0 to n-1.
{
Zm [region [I]-'a'] = I;
}
For (I = 0, k = 0; ch2 [I]! = '\ 0'; I + = 4) // string 2 processing, build Directed Graph
{
Edge [zm [ch2 [I]-'a'] [zm [ch2 [I + 2]-'a'] = 1;
To [zm [ch2 [I + 2]-'a'] ++; // vertex entry level + 1
}
DFS (0 );
// Printf ("\ n"); this way, the empty row poj can be output.
}
Return 0;
}
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <stdlib. h>
# Include <algorithm>
Using namespace std;
# Deprecision MAX 200
Char comment [MAX], ch2 [MAX], ch3 [MAX];
Int n, m, zm [MAX], edge [MAX] [MAX], to [MAX], visit [MAX], a [MAX];
Bool comp (char a, char B)
{
Return a <B;
}
Void DFS (int len)
{
Int I, j;
If (len = n) // if the number of layers equals the string length, the output is
{
For (I = 0; I <n; I ++)
{
Printf ("% c", outputs [a [I]);
}
Printf ("\ n ");
Return;
}
For (I = 0; I <n; I ++)
{
If (! Visit [I] &! To [I])
{
For (j = 0; j <n; j ++) // subtract 1 from the entry of the vertex connected to this vertex.
{
If (edge [I] [j] = 1)
{
To [j] --;
}
}
Visit [I] = 1; // mark this point for access
A [len] = I;
DFS (len + 1 );
For (j = 0; j <n; j ++) // restore the site: add the reduced inbound traffic back.
{
If (edge [I] [j] = 1)
{
To [j] ++;
}
}
Visit [I] = 0; // restore the site
}
}
Return;
}
Int main ()
{
Int I, k, pd = 0;
While (gets (ch3 ))
{
If (pd = 0)
Pd = 1;
Else
Printf ("\ n"); // empty lines are output between test cases
Gets (ch2 );
Memset (zm,-1, sizeof (zm ));
Memset (edge,-1, sizeof (edge ));
Memset (to, 0, sizeof ());
Memset (visit, 0, sizeof (visit ));
For (I = 0, n = 0; ch3 [I]! = '\ 0'; I ++) // remove Spaces
{
If (ch3 [I] = '')
Continue;
Pipeline [n ++] = ch3 [I];
}
Sort (sort, sort + n, comp); // sort
For (I = 0; I <n; I ++) // process string 1 and convert the letter to an edge from 0 to n-1.
{
Zm [region [I]-'a'] = I;
}
For (I = 0, k = 0; ch2 [I]! = '\ 0'; I + = 4) // string 2 processing, build Directed Graph
{
Edge [zm [ch2 [I]-'a'] [zm [ch2 [I + 2]-'a'] = 1;
To [zm [ch2 [I + 2]-'a'] ++; // vertex entry level + 1
}
DFS (0 );
// Printf ("\ n"); this way, the empty row poj can be output.
}
Return 0;
}
Method 2: Use the next_permutation () function in the STL library to locate all strings
Output the matching strings and remove the non-conforming strings.
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <stdlib. h>
# Include <algorithm>
Using namespace std;
# Deprecision MAX 200
Char comment [MAX], ch2 [MAX], ch3 [MAX];
Int n, m, zm [MAX], edge [MAX] [MAX], to [MAX], visit [MAX], a [MAX];
Bool comp (char a, char B)
{
Return a <B;
}
Int Topsort (char ch [])
{
Int I, j;
For (I = n-1; I> 0; I --)
{
For (j = I-1; j> = 0; j --)
{
If (edge [zm [ch [I]-'a'] [zm [ch [j]-'a'] = 1)
Return 0;
}
}
Return 1;
}
Int main ()
{
Int I, k, pd = 0;
While (gets (ch3 ))
{
If (pd = 0)
Pd = 1;
Else
Printf ("\ n ");
Gets (ch2 );
Memset (zm,-1, sizeof (zm ));
Memset (edge,-1, sizeof (edge ));
Memset (to, 0, sizeof ());
Memset (visit, 0, sizeof (visit ));
For (I = 0, n = 0; ch3 [I]! = '\ 0'; I ++) // trim the space
{
If (ch3 [I] = '')
Continue;
Pipeline [n ++] = ch3 [I];
}
Limit [n] = '\ 0 ';
Sort (sort, sort + n, comp); // sort
For (I = 0; I <n; I ++) // process string 1 and convert the letter to an edge from 0 to n-1.
{
Zm [region [I]-'a'] = I;
}
For (I = 0, k = 0; ch2 [I]! = '\ 0'; I + = 4) // string 2 processing, build Directed Graph
{
Edge [zm [ch2 [I]-'a'] [zm [ch2 [I + 2]-'a'] = 1;
To [zm [ch2 [I + 2]-'a'] ++;
}
If (Topsort (rule) // if the condition is met, the output is
Printf ("% s \ n", success );
While (next_permutation (partial, partial + n ))
{
If (Topsort (rule) // if the condition is met, the output is
Printf ("% s \ n", success );
}
// Printf ("\ n ");
}
Return 0;
}