ZCMU:1038: 二哥的困惑 Ⅰ

1038: 二哥的困惑 Ⅰ

Description
There is a programming language in which every program is a non-empty sequence of “<” and “>” signs and digits. Let’s explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts.

Current character pointer (CP);
Direction pointer (DP) which can point left or right;
Initially CP points to the leftmost character of the sequence and DP points to the right.

We repeat the following steps until the first moment that CP points to somewhere outside the sequence.

If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it’s erased from the sequence and the length of the sequence decreases by one.
If CP is pointing to “<” or “>” then the direction of DP changes to “left” or “right” correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is “<” or “>” then the previous character will be erased from the sequence.
If at any moment the CP goes outside of the sequence the execution is terminated.

It’s obvious the every program in this language terminates after some steps.

We have a sequence s1, s2, …, sn of “<”, “>” and digits. You should answer q queries. Each query gives you l and r and asks how many of each digit will be printed if we run the sequence sl, sl + 1, …, sr as an independent program in this language.

Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 100) — represents the length of the sequence s and the number of queries.

The second line contains s, a sequence of “<”, “>” and digits (0…9) written from left to right. Note, that the characters of s are not separated with spaces.

The next q lines each contains two integers li and ri (1 ≤ li ≤ ri ≤ n) — the i-th query.

Output
For each query print 10 space separated integers: x0, x1, …, x9 where xi equals the number of times the interpreter prints i while running the corresponding program. Print answers to the queries in the order they are given in input

Sample Input

7 4
1>3>22<
1 3
4 7
7 7
1 7

Sample Output

0 1 0 1 0 0 0 0 0 0
2 2 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
2 3 2 1 0 0 0 0 0 0

HINT
給你兩個指針,一個記錄當前位置(cp),一個記錄移動方向(dp)。然後重複執行兩個命令。輸出cp在整個程序中打印每個數的的個數。

思路:跟着題目模擬一遍。在處理第二個命令時,用一個變量記錄上一個方向字符的位置。處理第一個命令時,將這個變量歸零。

簡稱就是一開始往右邊走,遇到 > 或者 < 就往這個方向走,然後數字要減一。
Code:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
char x[105],y[105];
int main()
{
    int n,q;
    while(~scanf("%d %d",&n,&q))
    {
        scanf("%s",y);
        for(int i=0; i<q; i++)
        {
            int l,r;
            int s[10];
            memset(s,0,sizeof(s));
            memcpy(x,y,sizeof(y));
            scanf("%d %d",&l,&r);
            l--;
            r--;
            int p=n,d=1;
            for(int j=l; j>=l&&j<=r; j+=d)
            {
                if(x[j]>='0'&&x[j]<='9')
                {
                    s[x[j]-'0']++;
                    x[j]--;
                    p=n;
                }
                else if(x[j]=='<'||x[j]=='>')
                {
                    if(x[p]=='<'||x[p]=='>')
                        x[p]=0;
                    p=j;
                    if(x[j]=='<')
                        d=-1;
                    else
                        d=1;
                }
            }
            for(int j=0; j<10; j++)
            {
                if(j==0)
                    printf("%d",s[j]);
                else
                    printf(" %d",s[j]);
            }
            printf("\n");
        }
    }

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章