南大18本地夏令营题目

题目如下:

第一个题目,题解:需要好好考虑前序和后续的性质,考虑树链:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<vector>
using namespace std ;
#define MAX 1005
int pre[MAX];
int aft[MAX];
int stk[MAX];
int n ;
int main(){
    cin >> n ;
    for(int i = 0 ; i < n ; i ++)
    {
        cin >> pre[i];
    }
    for(int j = 0 ; j < n ; j ++)
    {
        cin >> aft[j] ;
    }
    int mx = 1 , top = 0, low = 0;
    for(int i = 0 ; i < n ; i ++ )
    {
        stk[++top] = pre[i];
        if(top > mx ) mx =top ;
        while(stk[top]== aft[low])
        {
            top--;
            low++;
        }
    }
    cout << mx <<endl;
}

第二个题目,问了一波大佬,又说用vector的,又说map的,肯定不能直接建图,那么我们就这么解决:感谢大佬支持

#include<map>
#include<queue>
#include<iostream>
#include<cstdio>
using namespace std ;
char str[6];

map<long long ,int> distances , exist;

long long starts, endss ;

long long encode(char *str)
{
    long long ans = 0 ;
    for(int i = 0 ; i < 5 ; i ++ )
    {
        if(i&&str[i-1]=='\0') str[i] = 0 ;
        ans <<=8;
        ans |= str[i];
    }
    return ans ;
}

void decode(char* str ,long long data)
{
    for(int i = 4 ; i >= 0  ; i --)
    {
        str[i]=data&255;
        data>>=8;
    }
}
void bfs();

int main(){
    cin >> str;
    starts = encode(str);
    cin >> str ;
    endss = encode(str);
    cout << 1 << endl ;
    while(~scanf("%s",str))
        exist[encode(str)]=1;
    cout <<"2"<<endl;
    bfs();
    cout << distances[endss]<<endl;
}

void bfs()
{
    queue<long long>Q ;
    distances[starts]= 1 ;
    Q.push(starts);
    while(!Q.empty())
    {
        long long now = Q.front();
        Q.pop();
        if(now== endss)
            break;
        char tmp[6];
        decode(tmp,now);
        for(int i = 0 ; i < 5 ; i ++)
        {
            char org = tmp[i];
            for(char j = 'a'; j <= 'z' ; j++)
            {
                tmp[i]= j ;
                long long ns = encode(tmp);
                if(exist[ns]&&!distances[ns])
                {
                    distances[ns]= distances[now]+1;
                    Q.push(ns);
                }
            }
            tmp[i] = org;
        }
    }
}


最后一个题目,就不写了01背包,没什么意思

猜你喜欢

转载自blog.csdn.net/qq_36616268/article/details/81150262