CSA Round #59 (Div. 2 only)

C.Triangular Matrix

题意

给n行字母,第n行有n个,(i,j)每次可以走到(i+1,j)或者(i+1,j+1),问从(1,1)开始走,字典序最小的序列(n<= 3000)

分析

很容易想到直接模拟,但会有很多重复的情况,我们只要从每个点出发统计一次就可以了,统计下一行有效位置有一个小技巧

#include <bits/stdc++.h>
using namespace std;

const int dim = 3002;

int nn;
vector<int>q;
vector<int>pos;
char answer[3010];
int cnt;
string s;

int main()
{
    scanf("%d",&nn);
    pos.push_back(0);
    //int n=nn;
    while(nn--)
    {

        cin>>s;
        int n=s.size();
        char ch='z';

        int poslength=pos.size();
        for(int i = 0; i < poslength; i++)
        {
            if(s[pos[i]] < ch)
            {
                ch=s[pos[i]];
            }
        }
        answer[cnt++]=ch;
        q.clear();
        for(int i = 0; i < poslength; i++)
        {
            if(s[pos[i]] == ch)
            {
                q.push_back(pos[i]);
            }
        }
        pos.clear();
        int qlength=q.size();
        for(int i = 0; i < qlength; i++)
        {
            if(pos.size() == 0 || pos.back() < q[i])
                pos.push_back(q[i]);
            pos.push_back(q[i]+1);
        }
    }
    answer[cnt]='';
    printf("%s
", answer);
    return 0;
}    
View Code

 

要么优秀要么生锈
原文地址:https://www.cnblogs.com/Superwalker/p/7930685.html