程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Codeforces Round #238 (Div. 2) B題

Codeforces Round #238 (Div. 2) B題

編輯:C++入門知識

字符串處理題

B. Domino Effect time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play withthe dominoes and make a "domino show".

Chris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction.

After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process.

\

Given the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process!<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KCgoKSW5wdXQKPHA+ClRoZSBmaXJzdCBsaW5lIGNvbnRhaW5zIGEgc2luZ2xlIGludGVnZXIgPGVtPm48L2VtPiAoMT+h3D88ZW0+bjwvZW0+P6HcPzMwMDApLAogdGhlIG51bWJlciBvZiB0aGUgZG9taW5vZXMgaW4gdGhlIGxpbmUuIFRoZSBuZXh0IGxpbmUgY29udGFpbnMgYSBjaGFyYWN0ZXIgc3RyaW5nPGVtPnM8L2VtPiBvZiBsZW5ndGggPGVtPm48L2VtPi4KIFRoZSA8ZW0+aTwvZW0+LXRoIGNoYXJhY3RlciBvZiB0aGUgc3RyaW5nIDxlbT5zPC9lbT48ZW0+aTwvZW0+IGlzCiBlcXVhbCB0bzwvcD4KPHVsPgo8bGk+Cg=="L", if the i-th domino has been pushed to the left;

  • "R", if the i-th domino has been pushed to the right;
  • ".", if the i-th domino has not been pushed.

    It is guaranteed that if si?=?sj?=?"L" and i?j, then there exists such k that i?k?j and sk?=?"R"; if si?=?sj?=?"R" and i?j, then there exists such k that i?k?j and sk?=?"L".

    Output

    Output a single integer, the number of the dominoes that remain vertical at the end of the process.

    Sample test(s) input
    14
    .L.R...LR..L..
    
    output
    4
    
    input
    5
    R....
    
    output
    0
    
    input
    1
    .
    
    output
    1
    
    Note

    The first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange.

    In the second example case, all pieces fall down since the first piece topples all the other pieces.

    In the last example case, a single piece has not been pushed in either direction.


    #include
    #include
    #include
    #define max 30001
    int n;int domin[max];
    char str[max];
    void f()
    {
        for(int i=1;i<=n;i++)
        {
            if(str[i]=='L'){
                for(int j=i;j>0;j--)
                {
                    domin[j]=1;
                    if(str[j]=='R')
                    {
                        if((i-j)%2==0)domin[(i+j)/2]=0;
                        break;
                    }
                }
            }
            if(str[i]=='R'){
                for(int j=i;j<=n;j++)
                {
                    domin[j]=1;
                    if(str[j]=='L')
                    {
                        if((j-i)%2==0)domin[(i+j)/2]=0;
                        i=j;
                        break;
                    }
                }
            }
        }
    }
    int main()
    {
        //freopen("input.txt","r",stdin);
        while(~scanf("%d\n",&n))
        {
            memset(domin,0,sizeof(domin));
            gets(str+1);
            f();
            int count=0;
            for(int i=1;i<=n;i++)if(!domin[i])count++;
            printf("%d\n",count);
        }
        return 0;
    }


    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved