题意:给定n个人的m个对话,问能不能找一个方式使得满足,上下楼层人名不同,并且自己不提及自己。

析:首先预处理每一层能有多少个user可选,dp[i][j] 表示第 i 层是不是可以选第 j 个user。最后再输出即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1LL << 60;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
map<string, int> mp;
int cnt;
vector<string> v;
bool vis[maxn][maxn];
int dp[maxn][maxn], a[maxn];
string name[maxn]; inline int getId(const string &s){
if(!mp.count(s)) name[mp[s] = cnt++] = s;
return mp[s];
} string getF(const string &t){
string s = "";
for(int j = 0; t[j] != ':'; ++j) s.push_back(t[j]);
return s;
} void calc1(bool *is, string s){
for(int i = 0; i < s.size(); ++i)
if(ispunct(s[i])) s[i] = ' ';
// cout << s << endl;
stringstream ss(s);
while(ss >> s) if(mp.count(s)) is[mp[s]] = true;
} int main(){
ios::sync_with_stdio(false);
int T; cin >> T;
while(T--){
cin >> n;
mp.clear();
cnt = 1;
string s;
memset(vis, 0, sizeof vis);
for(int i = 0; i < n; ++i){
cin >> s;
getId(s);
}
cin >> m;
cin.get();
v.clear();
v.push_back(" ");
for(int i = 1; i <= m; ++i){
getline(cin, s);
v.push_back(s);
calc1(vis[i], s);
a[i] = v[i][0] == '?' ? 0 : mp[getF(s)];
} memset(dp, 0, sizeof dp);
int tmp = 1;
dp[0][0] = 1;
for(int i = 1; i <= m; ++i){
int tt = 0;
if(a[i]){
dp[i][a[i]] = tmp > dp[i-1][a[i]];
tt = dp[i][a[i]];
}
else for(int j = 1; j <= n; ++j){
dp[i][j] = (!vis[i][j] && tmp > dp[i-1][j]);
tt += dp[i][j];
}
tmp = tt;
}
tmp = 0;
for(int i = 1; i <= n; ++i) if(dp[m][i]){ tmp = i; break; }
if(!tmp) cout << "Impossible" << endl;
else{
for(int i = m; i > 0; --i)
for(int j = 0; j <= n; ++j)
if(j != tmp && dp[i-1][j]){
if(!a[i]) a[i] = -tmp;
tmp = j;
break;
}
for(int i = 1; i <= m; ++i)
if(a[i] > 0) cout << v[i] << endl;
else cout << name[-a[i]] + v[i].substr(1) << endl;
}
}
return 0;
}

CodeForces 754C Vladik and chat (DP+暴力)的更多相关文章

  1. CodeForces 754C Vladik and chat ——(xjbg)

    虽然是xjbg的题目,但是并不很好做. 题意不难理解.读入有点麻烦.做法是先正着推每段对话的?可能是谁说的,然后反过来选择即可.正推时,其中vis数组表示谁已经被用过了,cnt表示该组当前可以选择几个 ...

  2. CodeForces 446A DZY Loves Sequences (DP+暴力)

    题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...

  3. Codeforces Round #390 (Div. 2) C. Vladik and chat(dp)

    http://codeforces.com/contest/754/problem/C C. Vladik and chat time limit per test 2 seconds memory ...

  4. codeforces 811E Vladik and Entertaining Flags(线段树+并查集)

    codeforces 811E Vladik and Entertaining Flags 题面 \(n*m(1<=n<=10, 1<=m<=1e5)\)的棋盘,每个格子有一个 ...

  5. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  6. Codeforces 811C Vladik and Memorable Trip (区间异或最大值) (线性DP)

    <题目链接> 题目大意: 给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都只能出现在这个区间. 每个区间的价值为该区间不同的数的异或值之和,现在问你这n个数最大的价值是 ...

  7. Codeforces 914 C 数位DP+暴力打表+思维

    题意 给出一个二进制数\(n\),每次操作可以将一个整数\(x\)简化为\(x\)的二进制表示中\(1\)的个数,如果一个数简化为\(1\)所需的最小次数为\(k\),将这个数叫做特殊的数, 问从\( ...

  8. 【dp】codeforces C. Vladik and Memorable Trip

    http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...

  9. Educational Codeforces Round 19 E. Array Queries(暴力)(DP)

    传送门 题意 给出n个数,q个询问,每个询问有两个数p,k,询问p+k+a[p]操作几次后超过n 分析 分块处理,在k<sqrt(n)时,用dp,大于sqrt(n)用暴力 trick 代码 #i ...

随机推荐

  1. maven打包配置

    maven打包配置,到底要打包哪些文件,如何配置?? <build> <finalName>weatherAdminSys</finalName> <plug ...

  2. date,datetime,timestamp 的区别

    date 表示年月日,如YY-MM-DD datetime 表示年月日和时间信息,如YY-MM-DD HH:MM:SS datestamp 和datetime表示的信息相同,但时间范围不同 时间范围 ...

  3. Java 笔试面试 算法编程篇 一

    方法 1 /* ********************************************************************************** 1.编写一个程序, ...

  4. push类型消息中间件-消息订阅者(一)

    1.订阅者的声明方式 我们以spring组件化的方式,声明一个消息订阅者,对于消息订阅者关心的主要有: topic: 一级消息类型(又名消息主题).如TRADE 消息类型:二级消息类型,区别同一Top ...

  5. linux下svn目录管理

    linux 下安装的svn目录文件: /data/svndata/winne/conf/passwd 是配置用户密码的文件路径

  6. B-number

    B-number 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 数位dp 这题是暑期集训的时候做的,昨天补了数位dp的记忆化搜索做法,把艾神的 ...

  7. Firebug入门指南

    据说,对于网页开发人员来说,Firebug是Firefox浏览器中最好的插件之一. 我最近就在学习怎么使用Firebug,网上找到一篇针对初学者的教程,感觉比较有用,就翻译了出来. ========= ...

  8. EasyuiAPI:基础

    一.EasyLoader(简单加载) locale属性:值类型是string locales属性:值类型是object 二.Draggable(拖动) 1.通过标签创建: <div id=&qu ...

  9. hdu_5881_Tea(xjb猜)

    题目链接:hdu_5881_Tea 题意: 有一壶水, 体积在 L 和 R 之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不 ...

  10. chapter6 深入了解函数

    Lua函数是具有特定词法域的第一类值,与其他传统类型的值(string and number)具有相同的权利. 它可以保存在变量和table中,也可以把它当参数传递,也可以作为返回值. 在Lua中有个 ...