Description
Children is used to playing with special cubes and letters written on Thecubes ' faces. The goal of the game is to compose words using such cubes. If youwant to compose the word "DOG", you must find 3 cubes, one containingthe letter ' D ', one containing the letter ' O ', And one containing the letter ' G ', and orient them so the proper letters is facing upward.
You is also given a some words, each element of which contains a word this youwould like to spell out using the cubes.
Input
There is several test Cases,each test case begins with both NUMBERSN (1<=N<=50) and M (1<=M<=50). The second line Contains N string,thei-th string shows the uppercase letters (between 2 and 50,inclusive) on the i-thcube. The third line contains the M words. Each element of words would containbetween 2 and uppercase letters, inclusive.
Output
Output the words ' number that can is composed using the given cubes inascending order. If no words ' number that can be composed output-1.
Sample Input
5 3
ABCDEF Defghi opqrst zzzzzz yyyyyy
CAT DOG PIZZA
6 7
ABCDEF Defghi opqrst Mnzlsa qeiogh iarjgs
DOG CAT MOUSE BIRD CHICKEN PIG ANIMAL
Sample Output
1
0 1 3 5
Hint
Case 1:
We can form the word "DOG" using ' D ' from the first cube, ' O ' fromthe third and ' G ' from the second. Note that if we had used the second cube toget ' D ' instead, we would is missing a ' G '.
Topic Analysis:
Test instructions: give you several multi-faceted bodies with uppercase letters on each face, so that you can judge the given string to be used to spell out the polygons given on it? The binary graph model can be constructed as follows: For each string, each letter is a point in set a, and each multipatch is a point in set B, and if each character in a is found on a cube in B, the cube has an edge with this point in a. Finally, check whether the maximum number of matches is a string length.
AC Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace Std;
const int maxn=52;
BOOL VIS[MAXN];
int LINK[MAXN];
String S[MAXN],T[MAXN];
Vector<int> G[MAXN]; Contiguous array Storage diagram
int m,n;
Vector<int> ans;
BOOL Dfs (int s) {
for (int i=0;i<g[s].size (); ++i) {
int u=g[s][i];
if (Vis[u]) continue;
Vis[u]=true;
if (Link[u]==-1 | | dfs (LINK[U])) {
Link[u]=s;
return true;
}
}
return false;
}
int hungry (int t) {
int ans=0;
memset (link,-1,sizeof (link));
for (int i=1;i<=t;++i) {
memset (vis,false,sizeof (VIS));
if (Dfs (i)) ++ans;
}
return ans;
}
int main ()
{
int i,j,k;
while (~SCANF ("%d%d", &n,&m)) {
Ans.clear ();
for (I=1;i<=n;++i)
cin>>s[i];
for (I=1;i<=m;++i)
cin>>t[i];
for (I=1;i<=m;++i) {
for (J=0;J<=MAXN;++J)
G[j].clear ();
int len=t[i].size ();
for (J=0;J<LEN;++J) {
for (K=1;k<=n;++k) {
String::size_typepos=s[k].find (T[i][j]);
if (Pos==string::npos) continue;
G[k].push_back (j+1);
}
}
if (Hungry (n) ==len)
Ans.push_back (i-1);
}
if (Ans.size () ==0) printf (" -1\n");
Else
For (I=0;i<ans.size (); ++i) {
if (i) printf ("");
printf ("%d", ans[i]);
}
printf ("\ n");
}
return 0;
eoj1854 Playing with Cubes binary graph Max match