zoukankan      html  css  js  c++  java
  • 2012年清华:玛雅人的密码

    题目描述:

    玛雅人有一种密码,如果字符串中出现连续的2012四个数字就能解开密码。给一个长度为N的字符串,(2=<N<=13)该字符串中只含有0,1,2三种数字,问这个字符串要移位几次才能解开密码,每次只能移动相邻的两个数字。例如02120经过一次移位,可以得到20120,01220,02210,02102,其中20120符合要求,因此输出为1.如果无论移位多少次都解不开密码,输出-1。

    输入:

    输入包含多组测试数据,每组测试数据由两行组成。
    第一行为一个整数N,代表字符串的长度(2<=N<=13)。
    第二行为一个仅由0、1、2组成的,长度为N的字符串。

    输出:

    对于每组测试数据,若可以解出密码,输出最少的移位次数;否则输出-1。

    样例输入:
    5
    02120
    样例输出:
    1
    思路:广搜,字符串长度为13,可用Tire为字符串做hash.
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    struct Trie{
        Trie *next[3];
        Trie()
        {
            for(int i=0;i<3;i++)
                next[i]=NULL;
        }
    }memory[50005];
    int tot;
    Trie *root;
    void Insert(char buf[])
    {
        int len=strlen(buf);
        Trie *p=root;
        for(int i=0;i<len;i++)
        {
            int k=buf[i]-'0';
            if(p->next[k]==NULL)
            {
                p->next[k]=&memory[tot++];
            }            
            p=p->next[k];
        }
    }
    bool Find(char buf[])
    {
        int len=strlen(buf);
        Trie *p=root;
        for(int i=0;i<len;i++)
        {
            int k=buf[i]-'0';
            if(p->next[k]==NULL)
            {
                return false;
            }
            p=p->next[k];
        }
        return true;
    }
    struct Node{
        char s[15];
        int step;
        Node(){}
        Node(char s[],int step)
        {
            strcpy(this->s,s);
            this->step=step;
        }
    };
    int len;
    char buf[15];
    void bfs()
    {
        queue<Node> que;
        que.push(Node(buf,0));
        Insert(buf);
        while(!que.empty())
        {
            Node now=que.front();que.pop();
            if(strstr(now.s,"2012")!=NULL)
            {
                printf("%d
    ",now.step);
                return ;
            }
            for(int i=0;i<len-1;i++)
            {
                char s[15]="";
                strcpy(s,now.s);
                char tmp=s[i];
                s[i]=s[i+1];
                s[i+1]=tmp;
                if(!Find(s))
                {
                    que.push(Node(s,now.step+1));
                    Insert(s);
                }
            }
        }
        printf("-1
    ");
    }
    int main()
    {    
        while(scanf("%d",&len)!=EOF)
        {
            scanf("%s",buf);
            tot=0;
            memset(memory,0,sizeof(memory));//将结构体中的成员变量全部置为0
            root=&memory[tot++];
            bfs();
        }
        return 0;
    }

     字符串hash

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int MOD=100007;
    struct Node{
        char s[15];
        int step;
        Node(){}
        Node(char s[],int step)
        {
            strcpy(this->s,s);
            this->step=step;
        }
    };
    int vis[100007];
    int len;
    char buf[15];
    int Hash(char s[])
    {
        unsigned int x=1;
        for(int i=0;s[i];i++)
        {
            x*=10;
            x+=(s[i]-'0');
        }
        return x%MOD;
    }
    void bfs()
    {
        memset(vis,0,sizeof(vis));
        queue<Node> que;
        que.push(Node(buf,0));
        vis[Hash(buf)]=1;
        while(!que.empty())
        {
            Node now=que.front();que.pop();
            if(strstr(now.s,"2012")!=NULL)
            {
                printf("%d
    ",now.step);
                return ;
            }
            for(int i=0;i<len-1;i++)
            {
                char s[15]="";
                strcpy(s,now.s);
                char tmp=s[i];
                s[i]=s[i+1];
                s[i+1]=tmp;
                int code=Hash(s);
                if(!vis[code])
                {
                    que.push(Node(s,now.step+1));
                    vis[code]=1;
                }
            }
        }
        printf("-1
    ");
    }
    int main()
    {    
        while(scanf("%d",&len)!=EOF)
        {
            scanf("%s",buf);
            bfs();
        }
        return 0;
    }
  • 相关阅读:
    在R语言中轻松创建关联网络
    在R语言中显示美丽的数据摘要summary统计信息
    R语言中不同类型的聚类方法比较
    R语言中的划分聚类模型
    R语言解释生存分析中危险率和风险率的变化
    Stata估算观测数据的风险比
    Stata 中Mata的st_view函数
    R语言多臂试验
    R语言使用倾向评分提高RCT(随机对照试验)的效率
    R语言在RCT中调整基线时对错误指定的稳健性
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5429049.html
Copyright © 2011-2022 走看看