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

来源:互联网 发布:淘宝处理不公平 编辑:程序博客网 时间:2024/05/01 05:33

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, Rconst 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
原创粉丝点击