2019湘潭賽題解 | E題 XTCPC (三種寫法)

這道題

在比賽的時候其實已經有思路了

但是在最後45分鐘的時候 隊友說我們就選一道題來做吧……

然後選了B題 (就是題意理解錯誤了的那題 so沒做出來)

放一下E題題面

 

E - Hello XTCPC

You have a string of lowercase letters.You need to find as many sequence “xtCpc” as possible.But letters in the same position can only be used once。 

Input

The input file contains two lines. 

The first line is an integer nn show the length of string.(1≤n≤2×1051≤n≤2×105) 

The second line is a string of length n consisting of lowercase letters and uppercase letters. 

Output

The input file contains an integer show the maximum number of different subsequences found. 

Sample Input

10
xtCxtCpcpc

Sample Output

2

賽後 

我們三個人都想補這題

其實比賽的時候已經有思路了 

但是沒有很好的實現

將 x t C p c 五個字母出現的位置保存起來

找到 x的位置 < t的位置 < C的位置 < p的位置 < c的位置

計數+1

繼續找下一個存在這樣關係的位置

以下是三個人的代碼

我的:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
char s[200050];
int main()
{
    int n;
    while (~scanf("%d",&n))
    {
        int x=0,t=0,C=0,p=0,c=0;
        scanf("%s",s);
        for (int i=0; i<n; i++)
        {
            if (s[i] == 'x')
                x++;
            else if (s[i]=='t' && x)
            {
                x--;
                t++;
            }
            else if (s[i]=='C' && t)
            {
                t--;
                C++;
            }
            else if (s[i]=='p' && C)
            {
                C--;
                p++;
            }
            else if (s[i]=='c' && p)
            {
                p--;
                c++;
            }
        }
        printf("%d\n",c);
    }
    return 0;
}

起初沒有想到這麼簡潔的思想

在看所有人的代碼的時候發現的

瞬間覺得的 怎麼這麼簡單!!

 

Alone寫的:

#include <iostream>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
using namespace std;
queue <int> x,t,C,p,c;
int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    while(~scanf("%d",&n))
    {
        getchar();
        while(!x.empty())
            x.pop();
        while(!c.empty())
            c.pop();
        while(!t.empty())
            t.pop();
        while(!p.empty())
            p.pop();
        while(!C.empty())
            C.pop();

        char s;
        for(int i=1; i<=n; i++)
        {
            scanf("%c",&s);
            // printf("%c**\n",s);
            if(s=='x')
                x.push(i);
            else if(s=='t')
                t.push(i);
            else if(s=='C')
                C.push(i);
            else if(s=='p')
                p.push(i);
            else if(s=='c')
                c.push(i);
        }

        int ans=0,sign=0;

        while(!x.empty())
        {
            int now1=x.front();
            while(!t.empty())
            {
               // cout << t.front() << " "<< now1<< endl;
                if(t.front()<=now1)
                    t.pop();
                else
                {
                    int now2=t.front();
                    while(!C.empty())
                    {
                        //cout << C.front() << " " << now2 <<endl;
                        if(C.front()<=now2)
                            C.pop();
                        else
                        {
                            int now3=C.front();
                            while(!p.empty())
                            {
                              //  cout << p.front() << " " << now3 << endl;
                                if(p.front()<=now3)
                                    p.pop();
                                else
                                {
                                    int now4=p.front();
                                    while(!c.empty())
                                    {
                                        //cout << c.front() << " "<< now4 <<endl;
                                        if(c.front()<=now4)
                                            c.pop();
                                        else
                                        {
                                            sign=1;
                                            ans+=1;
                                            c.pop();
                                            x.pop();
                                            t.pop();
                                            C.pop();
                                            p.pop();
                                            break;
                                        }
                                    }
                                    break;
                                }
                            }
                            break;
                        }
//                        break;
                    }
                    break;
                }
//                break;
            }
            if(sign!=1)
                break;
            if(sign)
            {
                sign=0;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

一看就覺得好嚇人

但 我還是把她的代碼改好了

(原來的代碼是錯的 她多寫了一個; 還多寫了兩個break 導致WA了7/8遍 心態都崩了 我就說 你別寫了 換個題目做……)

 

大白也WA了三四次…… 

我還沒改完 等待ing

 

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