zoukankan      html  css  js  c++  java
  • 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.csdn.net/h1021456873/article/details/54947748的代码。

      1 #include<iostream>
      2 #include<algorithm>
      3 #include<cstring>
      4 #include<cstdio>
      5 #include<sstream>
      6 #include<vector>
      7 #include<stack>
      8 #include<queue>
      9 #include<cmath>
     10 #include<map>
     11 #include<set>
     12 using namespace std;
     13 typedef long long ll;
     14 typedef pair<int,int> pll;
     15 const int INF = 0x3f3f3f3f;
     16 const int maxn=100+5;
     17 
     18 int n, m;
     19 
     20 int f[maxn][maxn];  //f[i][j]表示j可以说第i句话
     21 int g[maxn][maxn];  //记录状态,用于输出(相当于记录路径)
     22 
     23 string name[maxn];
     24 string str[maxn]; //第i句话
     25 string re[maxn];  //记录第i句话,不包括说话人
     26 
     27 map<string, int> ID;
     28 
     29 void print(int m, int x)
     30 {
     31     if(m==0)  return;
     32     print(m-1,g[m][x]);
     33     cout<<name[x]<<re[m-1]<<endl;
     34 }
     35 
     36 int main()
     37 {
     38     //freopen("in.txt","r",stdin);
     39     int t;
     40     scanf("%d",&t);
     41     while(t--)
     42     {
     43         ID.clear();
     44         bool flag=true;
     45 
     46         scanf("%d",&n);
     47         for(int i=0;i<n;i++)
     48         {
     49             cin>>name[i];
     50             ID[name[i]]=i;
     51         }
     52         scanf("%d",&m);
     53         getchar();
     54         for(int i=0;i<m;i++)
     55         {
     56             getline(cin, str[i]);
     57             re[i].clear();
     58         }
     59 
     60         memset(f,0,sizeof(f));
     61         memset(g,0,sizeof(g));
     62         f[0][n]=1;
     63         
     64         for(int i=0;i<m;i++)
     65         {
     66             string user, buf = str[i];
     67             int p=0;
     68             for(; p<buf.size() && buf[p]!=':'; p++)
     69             {
     70                 user+=buf[p];
     71             }
     72 
     73             int q = p;
     74             while(q<buf.size())
     75             {
     76                 re[i]+=buf[q++];
     77             }
     78 
     79             set<int> mention;  //记录当前这句话不能说的人
     80             while(p<buf.size())
     81             {
     82                 string word;
     83                 while(p<buf.size() && isalnum(buf[p]))
     84                 {
     85                     word+=buf[p++];
     86                 }
     87                 if(ID.count(word))  mention.insert(ID[word]);
     88                 if(p<buf.size())  p++;
     89             }
     90 
     91             if(user=="?")
     92             {
     93                 for(int j=0;j<=n;j++)
     94                 {
     95                     if(!f[i][j])  continue;  //寻找上一句可以说的人
     96                     for(int k=0;k<n;k++)
     97                     {
     98                         if(mention.count(k) || k==j)  continue;  //不在set中并且和上一句说话的不是同一个人
     99                         f[i+1][k]=1;
    100                         g[i+1][k]=j;  
    101                     }
    102                 }
    103             }
    104             else
    105             {
    106                 if(!ID.count(user))
    107                 {
    108                     flag=false;
    109                 }
    110                 int id = ID[user];
    111                 if(mention.count(id))
    112                 {
    113                     flag=false;
    114                 }
    115                 else
    116                 {
    117                     for(int j=0;j<=n;j++)
    118                     {
    119                         if(!f[i][j])  continue;
    120                         if(id!=j)
    121                         {
    122                             f[i+1][id]=1;
    123                             g[i+1][id]=j;
    124                         }
    125                     }
    126                 }
    127             }
    128         }
    129 
    130         if(flag==true)
    131         {
    132             int x=-1;
    133             for(int j=0;j<n;j++)
    134                 if(f[m][j])  x=j;
    135 
    136             if(x==-1)  puts("Impossible");
    137             else print(m,x);
    138         }
    139         else puts("Impossible");
    140     }
    141     return 0;
    142 }
  • 相关阅读:
    人生是马拉松,胜者不一定是跑得最快的
    二.JSP开发过程中遇到的问题及解决
    一.JSP开发的工具下载与环境搭建
    将文件隐藏到图片中的方法与一键合成工具
    VC6兼容性及打开文件崩溃问题解决
    windows10UWP开发真机调试时遇到DEP6100和DEP6200解决办法
    Vs2015 win10虚拟机启动问题:无法设置UDP端口 解决方法 合集(转载)
    10个维修中最常见的蓝屏代码,值得收藏!
    [idea]添加jar包的方法
    [idea]创建一个控制台程序
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/7079684.html
Copyright © 2011-2022 走看看