#1626 : 缩写命名
-
2 4 hiho oh i hate you 3 hiho happy input output
样例输出
-
Yes No
描述
很多计算机科学工作者都喜欢给自己发明的系统或者方法起个拉风的名字。他们一般会用若干个单词描述这个系统,并且从这些单词中各取出一个字母组合在一起作为系统的名字。如果组合在一起的名字恰好是个有意义的单词,那就更COOL了。
例如 "FAWN:a Fast Array of Wimpy Nodes" 以及 "EMILI: Energy Minimizing Idle Listening wIfi"。
现在给定一个包含N个单词的字典{W1, W2, ... WN}和一个目标单词S,能否从字典中选出|S|个不同的单词,并且从每个单词中取出一个字母组成单词S?
输入
输入包含多组数据。
第一行包含一个整数T,代表测试数据的组数。(1 ≤ T ≤ 20)
对于每组数据,第一行包含一个整数N。
第二行包含目标单词S。
以下N行每行一个单词Wi。
对于30%的数据,1 ≤ N ≤ 10
对于100%的数据,1 ≤ N ≤ 100, 1 ≤ |S| ≤ 100, 1 ≤ |Wi| ≤ 100, 所有单词只包含小写字母。
输出
对于每组数据,输出一行Yes或者No,代表是否能通过上述方法组成S。
裸的模板题,建图、匈牙利算法,二分图匹配。。。。WA到死,一直得0分,赛后检查代码,发现YES和NO我都大写了,题目要求小写。。。。。有毒啊
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
int T,N;
string S;
int V;
vector<int>G[209];
int match[209];
bool used[209];
bool haveEdge[109][109];
void addEdge(int a,int b){
G[a].push_back(b);
G[b].push_back(a);
}
bool dfs(int v){
used[v]=1;
for(int i=0;i<G[v].size();i++){
int u=G[v][i];
int w=match[u];
if(w<0||!used[w]&&dfs(w)){
match[v]=u;
match[u]=v;
return 1;
}
}
return 0;
}
int bipartite_matching(){
int res=0;
memset(match,-1,sizeof(match));
for(int i=0;i<V;i++){
if(match[i]<0){
memset(used,0,sizeof(used));
if(dfs(i))res++;
}
}
return res;
}
int main(){
cin>>T;
while(T--){
cin>>N;
cin>>S;
int Len=S.length();
V=Len+N;
for(int i=0;i<V;i++){
G[i].clear();
}
memset(haveEdge,0,sizeof(haveEdge));
for(int i=0;i<N;i++){
string W;
cin>>W;
for(int j=0;j<W.length();j++){
for(int k=0;k<Len;k++){
if(S[k]==W[j]){
if(!haveEdge[k][Len+i]){
addEdge(k,Len+i);
haveEdge[k][Len+i]=1;
}
}
}
}
}
if(bipartite_matching()==Len)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
//
// _oo0oo_
// o8888888o
// 88" . "88
// (| -_- |)
// 0\ = /0
// ___/`---'\___
// .' \\| |// '.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ - /// | |
// | \_| ''\---/'' |_/ |
// \ .-\__ '-' ___/-. /
// ___'. .' /--.--\ `. .'___
// ."" '< `.___\_<|>_/___.' >' "".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `_. \_ __\ /__ _/ .-` / /
// =====`-.____`.___ \_____/___.-`___.-'=====
// `=---='
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// 佛祖保佑 永无BUG
//
//
//