2016"百度之星" - 资格赛(Astar Round1)

A题

我用线段树进行处理,但是好像有错误,不过后来数据进行了修改

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define mid (L+R)/2
#define lson o<<1, L, mid
#define rson o<<1|1, mid+1, R
const int maxn = 100500;
const int MOD = 9973;
int cnt[maxn<<2];
int a[maxn];
char s[maxn];

void pushup(int o){
        cnt[o] = (cnt[o<<1]*cnt[o<<1|1])%MOD;
}

void build(int o,int L,int R)
{
    if(L == R)
    {
        cnt[o] = a[L];
        return ;
    }
    build(lson);
    build(rson);
    pushup(o);
}

int query(int o,int L,int R,int x,int y)
{
    if(x <= L && R <= y)
        return cnt[o];
    int ret = 1;
    if(x <= mid)
        ret = (ret*query(lson,x,y))%MOD;
    if(y > mid)
        ret = (ret*query(rson,x,y))%MOD;
    return ret;
}

int main()
{
    int N;
    while(scanf("%d", &N) != EOF)
    {
        scanf("%s", s);
        int len = strlen(s);
        for(int i = 0; i < len; i++)
            a[i+1] = s[i] - 28;
        build(1, 1, len);
        int a,b,temp;
        for(int i = 0; i < N; i++)
        {
            scanf("%d%d", &a, &b);
            if(a > b||a < 1||b < 1||b > len ||a > len)
                printf("%d\n", temp);
            else
            {
                temp = query(1,1,len,a,b);
                printf("%d\n", temp);
            }
        }
    }
    return 0;
}

B题

处理一下高精度加法,用数组存一下,可以看成上楼梯,每次上一层或者2层,于是可以总结出f[i] = f[i-1]+f[i-2].

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1005;
int a[maxn][maxn];

void init(int a[maxn][maxn])
{
    memset(a,0,sizeof(a));
    a[1][0] = 1;
    a[2][0] = 2;
    for(int i = 3; i <= 1002; i++)
    {
        for(int j = 0; j < maxn ; j++)
        {
            a[i][j] += a[i-1][j] + a[i-2][j];
            if(a[i][j] >= 10)
            {
                a[i][j] -= 10;
                a[i][j+1]++;
            }
        }
    }
}

int main()
{
    int n,i;
    init(a);
    while(scanf("%d", &n) != EOF)
    {
        for(i = 1000; i >= 0; i--)
            if(a[n][i]) break;
        for(; i >= 0; i--)
            printf("%d",a[n][i]);
        printf("\n");
    }
    return 0;
}

C题

字典树的题目,有时会漏一些情况,一种情况是,加了往字典树加he,hee,heee,heeee,删除hee后,查看he和h是否存在,另一种情况就是插入hee,hello,heee,删除hee,查看he和hell是否存在。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
const int maxnode = 3000000;
const int sigma_size = 27;

struct Node
{
    int x,y;
    Node(int a,int b):x(a),y(b){}
    Node() {}
};

struct Trie
{
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int sz;
    Trie() {sz = 1; memset(ch[0], 0, sizeof(ch[0]));}
    int idx(char c) {return c - 'a';}
    void Insert(char *s)
    {
        int u = 0, n = strlen(s);
        for(int i = 0; i < n; i++)
        {
            int c = idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 1;
                ch[u][c] = sz++;
                u = ch[u][c];
            }
            else
            {
                u = ch[u][c];
                val[u]++;
            }
        }
    }

    bool Search(char *s)
    {
        int u = 0,n = strlen(s);
        for(int i = 0; i < n; i++)
        {
            int c = idx(s[i]);
            if(!ch[u][c])
                return false;
            else u = ch[u][c];
        }
        return true;
    }

    void Delete(char *s)
    {
        stack <Node> S;
        while(!S.empty()) S.pop();
        int u = 0,n = strlen(s),f,c;
        for(int i = 0; i < n; i++)
        {
            c = idx(s[i]);
            S.push(Node(u,c));
            if(!ch[u][c])
                return;
            else
            {
                f = u;
                u = ch[u][c];
            }
        }
        ch[f][c] = 0;
        if(val[f] - val[u] == 0)
        {
            S.pop();
            while(!S.empty())
            {
                Node node = S.top();
                S.pop();
                int temp = ch[node.x][node.y];
 //               printf("%d %d\n", node.x,node.y);
                if(val[temp] - val[u]) break;
                else ch[node.x][node.y] = 0;
            }
        }
    }
};

Trie tree;

int main()
{
    int N;
    scanf("%d", &N);
    char op[10],s[35];
    while(N--)
    {
        scanf("%s %s", op, s);
        if(op[0] == 'i')
            tree.Insert(s);
        else if(op[0] == 's')
        {
            if(tree.Search(s)) printf("Yes\n");
            else printf("No\n");
        }
        else if(op[0] == 'd')
            tree.Delete(s);
    }
    return 0;
}

D题

仅仅是用STL中的map就可以解出来了

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 100005;
int has[maxn];
string str;
map <string, int> Map;

int main()
{
    int N;
    scanf("%d", &N);
    while(N--)
    {
        cin >> str;
        sort(str.begin(), str.end());
        if(Map[str])
        {
            printf("%d\n", Map[str]);
            Map[str]++;
        }
        else
        {
            printf("0\n");
            Map[str] = 1;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hybrid-Astar是一种路径规划算法,它结合了A*算法(Astar)和RS距离(Reeds-Shepp距离)来寻找车辆的最优路径。使用Matlab实现这个算法需要2016a版本或更高版本。 首先,Hybrid-Astar算法以车辆的运动学模型为节点,在搜索过程中使用A*算法来计算当前点到终点的最短路径。A*算法通过综合考虑实际代价(g(n))和启发式代价(h(n))来选择下一个节点。在这里,h(n)函数的估计代价是通过计算当前点到终点的Astar距离和RS距离两者中的最大值得到的。 其次,RS距离是一种考虑了车辆转弯半径的距离度量方式。它使用车辆的运动学模型来计算车辆在转弯过程中可能遇到的最小转弯半径,并将其作为路径规划的一个重要因素。 在Matlab中实现Hybrid-Astar算法,可以使用基本的路径规划算法框架,并结合A*算法和RS距离的计算来实现。具体实现步骤可以按照以下方式进行: 1. 定义车辆的运动学模型,包括车辆的位置、速度、加速度等参数。 2. 定义启发式函数h(n),根据当前点到终点的Astar距离和RS距离两者中的最大值来估计代价。 3. 使用A*算法进行路径搜索,考虑实际代价g(n)和启发式代价h(n),选择下一个节点。 4. 在搜索过程中,根据车辆的运动学模型和RS距离计算车辆在转弯过程中的最小转弯半径。 5. 循环执行路径搜索,直到找到终点或搜索完所有可能的节点。 6. 返回最优路径。 这是Hybrid-Astar算法在Matlab中的一种实现方式,你可以根据具体需求进行调整和优化。以上是关于Hybrid-Astar matlab实现的简要介绍。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [Hybrid-Astar(混合A星算法)路径规划MATLAB代码](https://download.csdn.net/download/weixin_56691527/87614899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [混合A星算法-Hybrid_Astar(matlab)](https://download.csdn.net/download/qq_49747343/13219383)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值