题意:给定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. Myeclipse2014中,新建部署Maven项目

    一.环境 1.1 myeclipse2014 1.2 maven3.2.1 1.3 jdk1.7 上述环境配置可参照我之前编写的maven搭建方面的文档.本文着重介绍myeclipse2014下mav ...

  2. @PostConstruct 和 @PreDestory

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  3. LSM树由来、设计思想以及应用到HBase的索引(转)

    转自: http://www.cnblogs.com/yanghuahui/p/3483754.html 讲LSM树之前,需要提下三种基本的存储引擎,这样才能清楚LSM树的由来: 哈希存储引擎  是哈 ...

  4. XSL相关

    <?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet type="tex ...

  5. Activity设置singleTask无法通过Intent获取值的问题

    AActivity跳转BActivity ,AActivity设置lauchmode = "SingleTask"的话,在getIntent无法获取BActivity里面的内容,无 ...

  6. SLF4J 使用简记

    SLF4J 使用简记 使用 SLF4J有一段时间了,在此作上些许记录,以作提示. 本文使用的实际实现的日志框架是 Log4j,所以使用 log4j.properties 文件 SLF4J 需要引入的j ...

  7. SSM

    今天内容安排 1:复习mybatis 2:复习springMVC 3:springMVC+spring+mybatis组合起来,搭建一个web应用开发的框架 4:用户管理系统,针对用户的CRUD操作, ...

  8. 用VulApps快速搭建各种漏洞环境

    项目主页 https://github.com/Medicean/VulApps 项目介绍 收集各种漏洞环境,统一采用 Dockerfile 形式.DockerHub 在线镜像地址 获取并使用相关镜像 ...

  9. js--3目运算符

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  10. 【Python】生成器、回溯和八皇后问题

    八皇后问题: 把N个皇后,放在N*N的棋盘上面,从第一行往下放,每个皇后占一行,同时,每个皇后不能处在同一列,对角线上,有多少种放置方法. 思路: 典型的回溯问题: 1.当要放置最后一个皇后时候,默认 ...