• Codeforce 239 B. Easy Tape Programming


    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.

    Examples

    input

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

    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 


    这是一道模拟题,模拟一个他叙述的过程就是一个指针一个方向标记遇到>改成向右的方向,遇见<改成向左,每次遇到数字输出数字并将数字减一,减到零就删去,遇到连续的两个方向就删去先访问的那一个,给定一个子区间,求0-9输出了几遍。

    #include<bits/stdc++.h>
    #define Swap(a,b) a^=b^=a^=b
    #define cini(n) scanf("%d",&n)
    #define cinl(n) scanf("%lld",&n)
    #define cinc(n) scanf("%c",&n)
    #define coui(n) printf("%d",n)
    #define couc(n) printf("%c",n)
    #define coul(n) printf("%lld",n)
    #define speed ios_base::sync_with_stdio(0);//切不可用scnaf;
    #define Max(a,b) a>b?a:b
    #define Min(a,b) a<b?a:b
    using namespace std;
    typedef long long ll;
    const int INF=0x3f3f3f3f;
    const int maxn=1e6+10;
    const double esp=1e-9;
    int m,n,x,y,lll[maxn],rr[maxn];
    int l[110];
    int r[110];
    int ans[10];
    string w;
    int a[10];
    
    int main()
    {
        cin>>n>>m>>w;
        while(m--)
        {
            int l,r;
            cin>>l>>r;
            memset(a,0,sizeof(a));
            string t=w.substr(l-1,r-l+1);
            int lll=0,rr=1;
            int E=t.size();
            while(lll>=0&&lll<E)
            {
                if(t[lll]>='0'&&t[lll]<='9')
                {
                    a[t[lll]-'0']++;
                    t[lll]--;
                    if(t[lll]<'0')
                    {
                        t.erase(lll,1);
                        if(rr<0)lll+=rr;
                    }
                    else  lll+=rr;
                }
                else
                {
                    if(t[lll]=='<') rr=-1;
                    else rr=1;
                    if(lll+rr>=0&&lll+rr<E&&(t[lll+rr]=='<'||t[lll+rr]=='>'))
                    {
                        t.erase(lll,1);
                        if(rr<0) lll+=rr;
                    }
                    else lll+=rr;
                }
            }
            for(int i=0;i<10; i++)
                cout<<a[i]<<' ';
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    EffectiveC#17--装箱和拆箱的最小化
    EffectiveC#16--垃圾最小化
    EffectiveC#15--使用using和try/finally来做资源清理
    NET基础课--对象的筛选和排序(NET之美)
    Objective-C浅拷贝和深拷贝
    IOS viewdidload 方法在 init 方法之前调用
    [iOS]为什么不要在init初始化方法里调用self.view
    为什么init方法里有self.view就会先跑viewdidload方法
    IOS开发中重写init方法使用需谨慎
    The file “XXX.app” couldn’t be opened because you don’t have permission to view it.
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798814.html
Copyright © 2020-2023  润新知