Abandoned Animal Kattis (vector+map思维)
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.
Input
The input starts with a line with a single integer 1≤N≤100,0001≤N≤100,000, the number of supermarkets in town. Then follows a line with an integer N≤K≤100,000N≤K≤100,000, after which KK lines follow with a space-separated integer ii (between 00 and N−1N−1) and a string SS (consisting of only lowercase letters, at most 1010), denoting that item SS is available at the ithith 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≤KM≤K, the number of items your sister has bought. Then follow MM lines, each with string TT, 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
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.
Sample Input 1 | Sample Output 1 |
---|---|
3 3 0 chocolate 1 icecream 2 cookies 3 chocolate cookies icecream |
impossible |
Sample Input 2 | Sample Output 2 |
---|---|
3 4 0 chocolate 1 icecream 2 cookies 2 chocolate 3 chocolate icecream cookies |
unique |
Sample Input 3 | Sample Output 3 |
---|---|
3 10 0 tomatoes 0 cucumber 1 tomatoes 2 tomatoes 2 cucumber 1 mustard 0 salt 2 salad 2 salt 2 mustard 5 tomatoes cucumber salad mustard salt |
ambiguous |
题意:
n个商店,k件不同店的不同商品,购物清单上m个物品。从商店0到商店n-1,问可不可以按照购物清单上的顺序买完所有东西
思路:
从商店0走到商店n-1,找到当前所找商品再找下一个,找完就a了
代码:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 map <string, int> mp; 6 vector <int> ve[100005]; 7 int a[100005], re[100005]; 8 9 int main() 10 { 11 int n, k, m, i, j, num, tot, cur_store, cur_i, flag; 12 char s[15]; 13 scanf("%d", &n); 14 scanf("%d", &k); 15 tot = 1; 16 for(i=0; i<k; i++) 17 { 18 scanf("%d %s", &num, s); 19 if(mp[s]==0) mp[s] = tot++; 20 ve[num].push_back(mp[s]); 21 } 22 scanf("%d", &m); 23 for(i=0; i<m; i++) 24 { 25 scanf("%s", s); 26 a[i] = mp[s]; 27 } 28 29 // for(i=0; i<n; i++) 30 // { 31 // printf("%d: ", i); 32 // for(j=0; j<ve[i].size(); j++) 33 // { 34 // printf("%d ", ve[i][j]); 35 // } 36 // printf("\n"); 37 // } 38 39 i = 0; 40 cur_store = 0; 41 cur_i = 0; 42 flag = 0; 43 44 while(i<m&&cur_store<n) 45 { 46 if(ve[cur_store][cur_i]==a[i]) 47 { 48 re[i] = cur_store; 49 i++; 50 cur_i = 0; 51 } 52 else 53 { 54 if(i-1>=0&&ve[cur_store][cur_i]==a[i-1]&&cur_store!=re[i-1]) 55 flag = 1; 56 cur_i++; 57 if(cur_i==ve[cur_store].size()) 58 { 59 cur_store++; 60 cur_i = 0; 61 } 62 } 63 } 64 65 if(i==m) 66 { 67 cur_store++; 68 cur_i = 0; 69 while(cur_store<n) 70 { 71 if(ve[cur_store][cur_i]==a[m-1]) 72 { 73 flag = 1; 74 break; 75 } 76 else 77 { 78 cur_i++; 79 if(cur_i==ve[cur_store].size()) 80 { 81 cur_store++; 82 cur_i = 0; 83 } 84 } 85 } 86 if(flag==0) printf("unique\n"); 87 else printf("ambiguous\n"); 88 } 89 else printf("impossible\n"); 90 return 0; 91 }