zoukankan      html  css  js  c++  java
  • 字符串转下标

    方法1:

    #include<cstring>
    #include<cstdio>
    using namespace std;

    struct Ball
    {
        char color[20];
        int num;
    }ball[1005];

    int cnt=1;

    void count_num(char ch[])
    {
        int i;
        for(i=0;i<cnt;i++)
        {
            if(strcmp(ch,ball[i].color)==0)//遍历结构体数组,看前面是否出现过相同的字符串
            {
                ball[i].num++;
                return ;
            }

        }
        if(i==cnt)
        {
           strcpy(ball[cnt].color,ch);
           ball[cnt].num++;
           cnt++;
        }
    }

    int main()
    {
        int n,i;
        char ch[20];
        while(scanf("%d",&n)&&n)
        {
            for(int j=0;j<n;j++)
                ball[j].num=0;
            scanf("%s",ch);
            strcpy(ball[0].color,ch);
            ball[0].num=1;
            cnt=1;
            for(i=1;i<n;i++)
            {
                scanf("%s",ch);
                count_num(ch);
            }
            int Max=-1;
            int index=-1;
            for(int i=0;i<n;i++)
            {
                printf("%s:%d ",ball[i].color,ball[i].num);
                if(Max<ball[i].num)
                {
                    Max=ball[i].num;
                    index=i;
                }
            }
            printf("%s ",ball[index].color);
        }
        return 0;
    }

    方法2:

    map容器

    map<string,int> Hash

    map初始化为0,如果第一个关键字Hash[string]为0,则说明该关键字第一次出现,反之,如果不为0,则说明前面出现过

    #include <cstdio>
    #include <map>
    #include <string>
    #include <iostream>
    using namespace std;

    map<string, int> Hash;
    int par[1000005];

    void init(int n)
    {
        for(int i=0;i<n;i++)
        {
            par[i]=i;
        }
    }

    int Find(int x)
    {
        if(par[x]==x)
        {
            return x;
        }
        return par[x]=Find(par[x]);
    }

    void Unite(int x,int y)
    {
        x=Find(x);
        y=Find(y);
        if(x==y)
            return ;
        else{
            par[y]=x;
        }
    }


    int main () {
        int n, a, cnt = 1;
        string x, y;
        scanf("%d",&n);
        getchar();
        init(1000005);
        while (n --) {
            cin>>a>>x>>y;
            if (a == 0) {
                if (!Hash[x]) {
                    Hash[x] = cnt ++;
                }
                if (!Hash[y]) {
                    Hash[y] = cnt ++;
                }
                Unite (Hash[x], Hash[y]);
            } else {
                if (!Hash[x]) {
                    Hash[x] = cnt ++;
                }
                if (!Hash[y]) {
                    Hash[y] = cnt ++;
                }
                if (Find(Hash[x]) == Find (Hash[y])) {
                    printf("yes ");
                } else {
                    printf("no ");
                }
            }
        }
        return 0;
    }

  • 相关阅读:
    filter&map&reduce
    Linux通过进程ID查看文件路径
    PyCharm使用最多也最常用默认快捷键介绍
    Python中的深浅拷贝
    类加载器&反射
    Java web.xml 配置详解
    SpringMVC + Spring + MyBatis 整合 + Spring shrio + easyUI + 权限管理框架,带shrio session和shrio cache集群实现方案
    JAVA大数据数组排序
    高访问量WEB开发中的架构模式,学习从点滴开始
    WEB项目会话集群的三种办法
  • 原文地址:https://www.cnblogs.com/unknownname/p/7792640.html
Copyright © 2011-2022 走看看