pat1022__字符串查找

主要是对字符串的查找,为了方便并且快速的实现查找,用map会比较方便

同时如何把一个带有空格的字符串变成多个单词也有一个小技巧

char *point=book[i].keyWord;//关键词分离

            while(*point){

    sscanf(point,"%s",str);

    point+=strlen(str)+1;

    string stemp(str);

    mm_keyWord[stemp].push_back(i);

}
pat1022__字符串查找
#include<stdio.h>

#include<iostream>

#include<queue>

#include<map>

#include<vector>

#include<string>

#include<algorithm>

#include<string.h>

#include<algorithm>

using namespace std;



struct BOOK{

    char ID[9];

    char bname[89];

    char rname[89];

    char keyWord[89];

    char publisher[89];

    char year[6];

}book[10099];





int cmp(BOOK x,BOOK y){

    return strcmp(x.ID,y.ID)<0;

}



int main(){

    int n,m;

    while(scanf("%d",&n)!=EOF){

        int i;

        map<string,vector<int>>mm_bname;

        map<string,vector<int>>mm_rname;

        map<string,vector<int>>mm_keyWord;

        map<string,vector<int>>mm_publisher;

        map<string,vector<int>>mm_year;



        for(i=1;i<=n;i++){

            scanf("%s",book[i].ID);getchar();

            gets(book[i].bname);

            gets(book[i].rname);

            gets(book[i].keyWord);

            gets(book[i].publisher);

            scanf("%s",&book[i].year);

        }

        sort(&book[1],&book[1+n],cmp);

        //key word

        char str[19];

        for(i=1;i<=n;i++){

            string s2(book[i].bname);

            mm_bname[s2].push_back(i);

            string s3(book[i].rname);

            mm_rname[s3].push_back(i);

            string s4(book[i].publisher);

            mm_publisher[s4].push_back(i);

            string s5(book[i].year);

            mm_year[s5].push_back(i);



            char *point=book[i].keyWord;//关键词分离

            while(*point){

                sscanf(point,"%s",str);

                point+=strlen(str)+1;

                string stemp(str);

                mm_keyWord[stemp].push_back(i);

            }

        }



        int num,j;

        scanf("%d",&m);

        for(i=1;i<=m;i++){

            scanf("%d: ",&num);

            gets(str);

            string s1(str);

            printf("%d: %s\n",num,str);

            if(num==1){

                for(j=0;j<mm_bname[s1].size();j++){

                    printf("%s\n",book[mm_bname[s1][j]].ID);

                }

            }

            if(num==2){

                for(j=0;j<mm_rname[s1].size();j++){

                    printf("%s\n",book[mm_rname[s1][j]].ID);

                }

            }

            if(num==3){

                for(j=0;j<mm_keyWord[s1].size();j++){

                    printf("%s\n",book[mm_keyWord[s1][j]].ID);

                }

            }

            if(num==4){

                for(j=0;j<mm_publisher[s1].size();j++){

                    printf("%s\n",book[mm_publisher[s1][j]].ID);

                }

            }

            if(num==5){

                for(j=0;j<mm_year[s1].size();j++){

                    printf("%s\n",book[mm_year[s1][j]].ID);

                }

            }

            if(j==0){

                printf("Not Found\n");

            }

        }

    }



    return 0;

}
View Code

 

你可能感兴趣的:(字符串)