Description
You have a magic bottle. You can often present a magic bottle to others ......
The magic is as follows:
You have a glass screen similar to a test tube (larger than a test tube). Now you can inject x ml y-colored magic liquid into it (this color may be r, y, b) Where is the magic of this liquid?
After the magic bottle is injected, if there is no liquid in the bottle, it will precipitate in the bottom layer;
If there is liquid in the bottle, the judgment is: if the color is the same as the color of the lower layer, it will be integrated into one piece, for example (from the top down) the liquid level color is r y B r. After the r color liquid is poured, the color level is r y B R, but the volume of the top layer changes to the sum of 2. If the color is different from the color of the lower layer, an independent layer will be generated to overwrite the lower layer. For example, after r y B r is poured into the y color, program y r y B R.
The magic of this magic bottle is that when a liquid is poured out, it will only completely pour out the liquid on the outermost layer. After an inverted operation, the liquid layer will never be left or the next liquid layer will be poured out.
After several operations, we hope that you can show the liquid level distribution in the magic bottle, that is, to report the color and volume of each layer of liquid from the upper layer to the lower layer.
-
Input
-
The first line contains an integer T, indicating that there are T groups of test data.
The format of each group of test data is as follows:
The first line contains an integer N, indicating that there are N operations.
The following n rows are constructed in the following two cases:
Put y X indicates the liquid X milliliters in the Y color, where Y is one of r y B and X is an integer.
Get indicates the top liquid.
Ensure data validity.
-
Output
-
For each group of data, the liquid sequence after N operations is output, as described in the topic.
-
Sample Input
-
1
-
4
-
PUT R 10
-
PUT B 10
-
PUT B 10
-
GET
-
Sample output
-
R 10
Implement with Stack
#include <stdio.h>main(){int number,te;int op;int i;int j; char temp;int n;int up;char b[1000];int a[1000];scanf("%d",&number);for(te=1;te<=number;te++){up=0;scanf("%d",&op);for(i=0;i<op;i++){ getchar();scanf("%c",&temp);if(temp=='P'){for(j=0;j<3;j++)scanf("%c",&temp);scanf("%c",&temp);scanf("%d",&n);if(up==0){ a[up]=n; b[up]=temp; up++;}else{if(b[up-1]==temp)a[up-1]+=n;else{ a[up]=n; b[up]=temp; up++;} }}if(temp=='G'){printf("%c %d\n",b[0],a[0]);for(j=0;j<up-1;j++){a[j]=a[j+1];b[j]=b[j+1];up--;}}}}}