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

时间:2023-03-09 13:29:36
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 limit per test

256 megabytes

input

standard input

output

standard output

Recently Vladik discovered a new entertainment — coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them.

At first, he need to download t chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages.

Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages.

He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats!

Input

The first line contains single integer t (1 ≤ t ≤ 10) — the number of chats. The t chats follow. Each chat is given in the following format.

The first line of each chat description contains single integer n (1 ≤ n ≤ 100) — the number of users in the chat.

The next line contains n space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters.

The next line contains single integer m (1 ≤ m ≤ 100) — the number of messages in the chat. The next m line contain the messages in the following formats, one per line:

  • <username>:<text> — the format of a message with known sender. The username should appear in the list of usernames of the chat.
  • <?>:<text> — the format of a message with unknown sender.

The text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!'(exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters.

We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text "Vasya, masha13 and Kate!" can mention users "Vasya", "masha13", "and" and "Kate", but not "masha".

It is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender.

Output

Print the information about the t chats in the following format:

If it is not possible to recover senders, print single line "Impossible" for this chat. Otherwise print m messages in the following format:

<username>:<text>

If there are multiple answers, print any of them.

题意:

现在有n个人,然后有m句话,有些话不知道说话人是谁,现在我们要去找能去说这句话的人,要求是前后两句话说话的人不能相同,如果后面话中出现的名字也不能作为说话人。输出一种情况即可。

思路:

用 f【i】【j】=1 表示 j 可以说第 i 句话,g【】【】用来记录路径,用于最后的输出。

先对每句话进行处理,分离出说话人和后面话中所包含的说话人。对于这句话,我们找到上一句话可以说话的人,在此基础上,判断接下来可以说话的人有谁。

话说这个字符串的处理还真是麻烦啊....

参考了http://blog.****.net/h1021456873/article/details/54947748的代码。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m; int f[maxn][maxn]; //f[i][j]表示j可以说第i句话
int g[maxn][maxn]; //记录状态,用于输出(相当于记录路径) string name[maxn];
string str[maxn]; //第i句话
string re[maxn]; //记录第i句话,不包括说话人 map<string, int> ID; void print(int m, int x)
{
if(m==) return;
print(m-,g[m][x]);
cout<<name[x]<<re[m-]<<endl;
} int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
ID.clear();
bool flag=true; scanf("%d",&n);
for(int i=;i<n;i++)
{
cin>>name[i];
ID[name[i]]=i;
}
scanf("%d",&m);
getchar();
for(int i=;i<m;i++)
{
getline(cin, str[i]);
re[i].clear();
} memset(f,,sizeof(f));
memset(g,,sizeof(g));
f[][n]=; for(int i=;i<m;i++)
{
string user, buf = str[i];
int p=;
for(; p<buf.size() && buf[p]!=':'; p++)
{
user+=buf[p];
} int q = p;
while(q<buf.size())
{
re[i]+=buf[q++];
} set<int> mention; //记录当前这句话不能说的人
while(p<buf.size())
{
string word;
while(p<buf.size() && isalnum(buf[p]))
{
word+=buf[p++];
}
if(ID.count(word)) mention.insert(ID[word]);
if(p<buf.size()) p++;
} if(user=="?")
{
for(int j=;j<=n;j++)
{
if(!f[i][j]) continue; //寻找上一句可以说的人
for(int k=;k<n;k++)
{
if(mention.count(k) || k==j) continue; //不在set中并且和上一句说话的不是同一个人
f[i+][k]=;
g[i+][k]=j;
}
}
}
else
{
if(!ID.count(user))
{
flag=false;
}
int id = ID[user];
if(mention.count(id))
{
flag=false;
}
else
{
for(int j=;j<=n;j++)
{
if(!f[i][j]) continue;
if(id!=j)
{
f[i+][id]=;
g[i+][id]=j;
}
}
}
}
} if(flag==true)
{
int x=-;
for(int j=;j<n;j++)
if(f[m][j]) x=j; if(x==-) puts("Impossible");
else print(m,x);
}
else puts("Impossible");
}
return ;
}