[思维]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 fluffy 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 figure out at what store she bought what, and then you’ll reunite her with her counterpart in no time! However, you soon find 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: firstly 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 figure out at what store she bought what, and then you’ll reunite her with her counterpart in no time! However, you soon find 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: firstly 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,000, 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
思路:
能不能买到就是扫一遍,不能买到就是Impossible
是不是唯一方案就是通过两种卖法进行路径比较
(1)如果能早买就尽可能早卖,记录路径
(2)能晚买就尽可能晚买,同样记录路径
1 #include <iostream> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int maxn = 1e5+50; 5 map<string,int> mapp; 6 vector<int> shop[maxn]; 7 int buy[maxn]; 8 int no,n,k,m,now,all; 9 int suq1[maxn],suq2[maxn]; 10 char th[15]; 11 void init() 12 { 13 memset(suq1,-1,sizeof(suq1)); 14 memset(suq2,-1,sizeof(suq2)); 15 no=0; 16 cin>>n>>k; 17 for(int i=0;i<k;i++) 18 { 19 scanf("%d %s",&now,th); 20 if(mapp.count(th)) 21 { 22 shop[now].push_back(mapp[th]); 23 } 24 else 25 { 26 mapp[th]=no; 27 shop[now].push_back(no++); 28 } 29 } 30 cin>>m; 31 for(int i=0;i<m;i++) 32 { 33 scanf("%s",th); 34 buy[i]=mapp[th]; 35 } 36 } 37 int main() 38 { 39 init(); 40 all=0; 41 int tot=0; 42 for(int i=0;i<n&&tot<m;i++) 43 { 44 int len=shop[i].size(); 45 for(int j=0;j<len;j++) 46 { 47 if(shop[i][j]==buy[tot]) 48 { 49 suq1[tot]=i; 50 tot++; 51 i--; 52 break; 53 } 54 } 55 } 56 if(suq1[m-1]!=-1) 57 all++; 58 tot=m-1; 59 for(int i=n-1;i>=0&&tot>=0;i--) 60 { 61 int len=shop[i].size(); 62 for(int j=len-1;j>=0;j--) 63 { 64 if(shop[i][j]==buy[tot]) 65 { 66 suq2[tot]=i; 67 tot--; 68 i++; 69 break; 70 } 71 } 72 } 73 if(suq2[0]!=-1) 74 all++; 75 if(all==0) 76 cout << "impossible" <<endl; 77 else if(all==1) 78 cout << "unique" << endl; 79 else 80 { 81 bool flag=true; 82 for(int i=0;i<m;i++) 83 { 84 if(suq1[i]!=suq2[i]) 85 { 86 flag=false; 87 break; 88 } 89 } 90 if(flag) 91 cout << "unique" << endl; 92 else 93 cout << "ambiguous" << endl; 94 } 95 //cout << "Hello world!" << endl; 96 return 0; 97 } 98 /* 99 3 100 4 101 0 chocolate 102 0 icecream 103 1 icecream 104 2 cookies 105 3 106 chocolate 107 icecream 108 cookies 109 */
吐槽一下:最开始假设了一家店会有10000种商品,然后比较糟糕的情况就是1e5都扫一遍,这样是绝对T的,所以没敢这么写的,赛后看了大佬的代码,果然大佬还是强啊,不仅代码写的强,代码风格都压我