zoukankan      html  css  js  c++  java
  • poj 3623 Best Cow Line, Gold

    题目不算难,但是不认真想的话很容易wa,我就是wa了多次才意识到自己想法存在的缺陷。

    相同的时候往后找知道出现不相同时,只能判断出当前字符的优先顺序。

    这个题目如果朴素的按照这种方法做的话复杂度其实是n*n的,可是数据较弱,可以过,我用的就是朴素的办法。

    但是多加思索的话可以发现我们可以用后缀数组保存原串和反串,使复杂度降低到nlongn。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int maxn=3e4+9;
    char a[maxn],ans[maxn];
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=1;i<=n;i++)
            {
                char tmp[10];
                scanf("%s",tmp);
                a[i]=tmp[0];
            }
            int st=1,ed=n,lon=0;
            while(st<=ed)
            {
                if(a[st]<a[ed])
                ans[++lon]=a[st++];
                else if(a[st]>a[ed])
                ans[++lon]=a[ed--];
                else
                {
                    int s=st,t=ed;
                    while(s<=t&&a[s]==a[t])
                    {
                        s++;
                        t--;
                    }
                    if(s>t||a[s]<a[t])
                    ans[++lon]=a[st++];
                    else 
                    ans[++lon]=a[ed--];
                    
                }
            }
            for(int i=1;i<=lon;i++)
            {
                printf("%c",ans[i]);
                if(i%80==0)
                printf("
    ");
            }
            if(n%80)
            printf("
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    寒号鸟不是鸟,爸爸你会吃。
    思杨的课外班的思考
    一年级第二学期
    City
    SON
    python(16)——生成器
    python(15)——迭代和迭代器
    python(14)——python中的数学模块
    python(13)——lambda表达式
    Python(12)——变量作用域及闭包操作
  • 原文地址:https://www.cnblogs.com/pangblog/p/3294089.html
Copyright © 2011-2022 走看看