eoj1854 Playing with Cubes binary graph Max match

Source: Internet
Author: User

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.

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 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
6 7
ABCDEF Defghi opqrst Mnzlsa qeiogh iarjgs

Sample Output
0 1 3 5

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;



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;


if (Link[u]==-1 | | dfs (LINK[U])) {


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)


for (I=1;i<=m;++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");


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

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.