Abandoned Animal
题目描述
Your little sister has been a big help today: she went into town to do all the groceries! During this grand voyage,she was accompanied by her ?uffy friend,Mr. Fluffynose the Stuffed Animal. However,after her return,it seems that she has left him somewhere along the route! This is devastating news for your little sister,and as she won’t stop crying about it,you decide to retrace her steps through town.
You kNow that your sister will hold on to her beloved Fluffynose whenever possible,so the only time she Could’ve lost it is when she grabbed an item on her shopping list. So,all you have to do is ?gure out at what store she bought what,and then you’ll reunite her with her counterpart in no time! However,you soon ?nd out that this isn’t quite as easy as you thought: she went to a lot of stores,and although she kNows the names of the stores she went to and the order in which she visited them,she does not recall what she bought at each store (it Could have been nothing!). It would take a lot of time to blindly search all the stores for all these items. As you have better things to do today,like solving programming problems,you want to spend as little time on this retrieval as possible. Therefore,you want to kNow exactly which items your sister bought at each store before you start your search.
For this you have two pieces of information: ?rstly you kNow the inventory of all stores your sister went to.Secondly,you kNow exactly in what order she purchased the groceries,as she has very carefully stacked all items into her bag. You decide to number the stores your sister visited according to the order in which she visited them. Given this information,you want to decide whether you kNow for sure where she bought every item so you can retrace her steps as efficiently as possible.
You kNow that your sister will hold on to her beloved Fluffynose whenever possible,so the only time she Could’ve lost it is when she grabbed an item on her shopping list. So,all you have to do is ?gure out at what store she bought what,and then you’ll reunite her with her counterpart in no time! However,you soon ?nd out that this isn’t quite as easy as you thought: she went to a lot of stores,and although she kNows the names of the stores she went to and the order in which she visited them,she does not recall what she bought at each store (it Could have been nothing!). It would take a lot of time to blindly search all the stores for all these items. As you have better things to do today,like solving programming problems,you want to spend as little time on this retrieval as possible. Therefore,you want to kNow exactly which items your sister bought at each store before you start your search.
For this you have two pieces of information: ?rstly you kNow the inventory of all stores your sister went to.Secondly,you kNow exactly in what order she purchased the groceries,as she has very carefully stacked all items into her bag. You decide to number the stores your sister visited according to the order in which she visited them. Given this information,you want to decide whether you kNow for sure where she bought every item so you can retrace her steps as efficiently as possible.
输入
The input starts with a line with a single integer 1 ≤ N ≤ 100,000,the number of supermarkets in town. Then follows a line with an integer N ≤ K ≤ 100,after which K lines follow with a space-separated integer i (between 0 and N − 1) and a string S (consisting of only lowercase letters,at most 10),denoting that item S is available at the ith store that your sister visited. It is guaranteed that every store has at least one item,every item is available
at at least one store,and that every item occurs at most once at every store.
The second part of the input contains the list of items your sister bought,in order of purchase.It starts with a line with an integer M ≤ K,the number of items your sister has bought. Then follow M lines,each with string T,denoting the name of the item your sister bought. The items are given in the order she purchased them in. All items that your sister has bought are unique.
at at least one store,and that every item occurs at most once at every store.
The second part of the input contains the list of items your sister bought,in order of purchase.It starts with a line with an integer M ≤ K,the number of items your sister has bought. Then follow M lines,each with string T,denoting the name of the item your sister bought. The items are given in the order she purchased them in. All items that your sister has bought are unique.
输出
Output “impossible” if there is no path through the stores that matches your sister’s description.
Output “unique” if there is exactly one path through the stores that matches.
Output “ambiguous” if there are multiple possible paths.
Output “unique” if there is exactly one path through the stores that matches.
Output “ambiguous” if there are multiple possible paths.
样例输入
3
3
0 chocolate
1 icecream
2 cookies
3
chocolate
cookies
icecream
样例输出
impossible
样例输入
3
4
0 chocolate
1 icecream
2 cookies
2 chocolate
3
chocolate
icecream
cookies
样例输出
unique
来源/分类
英语确实是硬伤,六级阅读理解题
题意:一共有n 个商店(标号为 0 --> n-1),每个商店有多种商品,给一个购物单,问是否能按照顺序从 0 -- > n-1 商店购买,如果有多条路径输出 ambiguous,只有一条输出 unique,没有输出 impossible
题解:首先将字符串用 map 映射 到 1 --> tot,先将每个商品存到对应的商店 ,然后枚举商店,判断该商店中是否存在购物单中的商品,有就往下买,这样能判断是否有路径,我们可以加一个pre 记录前面一个商品 , 如果该商品也存在,就说明存在多路径,说的可能不明白,请参考代码
AC code:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 10; map<string,int>Hash; vector<int>shop[N]; int menu[N]; int tot = 0; int path(int num,int name) { int l = 0,r = shop[num].size() - 1; while(l <= r) { int mid = (l+r)>>1; if(shop[num][mid] < name) l = mid + 1; else if(shop[num][mid] > name) r = mid - 1; else return 1; } return 0; } int main() { int n,m,k,id; string name; cin>>n>>k; while(k--) { cin>>id>>name; if(!Hash[name]) Hash[name] = ++tot; shop[id].push_back(Hash[name]); } for(int i = 0;i < n; i++){ sort(shop[i].begin(),shop[i].end()); // for(int j = 0;j < shop[i].size();j++) // printf("%d%c",shop[i][j],j == shop[i].size()-1?‘\n‘:‘ ‘); } cin>>m; menu[0] = 0; for(int i = 1;i <= m;i++) { cin>>name; menu[i] = Hash[name]; } int goodnum = 1,flag1 = 1,flag2 = 0,pre = 0; for(int i = 0;i < n;i++) { if(path(i,pre)) flag2 = 1; while(goodnum <= m) { if(!path(i,menu[goodnum])) break; goodnum++; } pre = menu[goodnum - 1]; } if(goodnum <= m) flag1 = 0; // cout<<"flag1:"<<flag1<<" falg2"<<flag2<<endl; if(flag2&&flag1) puts("ambiguous"); else if(!flag2&&flag1) puts("unique"); else puts("impossible"); return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。