字符串输入输出

#include <cstring>

#include <stdio.h>

#include <string>

#include <cmath>  

#include <queue>   

#include <map>  

#include <memory>

#include <iostream>

using namespace std;

map<string, int> bmap, gmap;

string bname[501], gname[501];

int bpre[501[501];    //used to store boys' prefrence, bpre[i][j] = k means to boy i, he ranks the girl k in jth place, the girl k is his jth choice.

int gpre[501][501];    //used to store girls' prefrence, gre[i][j] = k means to girl i, she ranks the boy j in the kth place

                    //(different from the storing way of bpre[][], because to boys, they traverse girls from highest to lowest; 

                    //but to girls, they need to quickly get to know one boy's rank)

int gres[501];    //used to store girls' result, gres[i]=j means girl i is with boy j

int bres[501];

char input[1500];

int n;

queue<int> bqueue;



int main(){

    while(~scanf("%d",&n)){

        memset(gres,-1,sizeof gres);

        memset(bres,-1,sizeof bres);

        getchar();

        bmap.clear();

        gmap.clear();

        int gcount = 0, i = 0, j = 0;

        int index = 0;

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

            scanf("%s", input);

            bname[i] = input;

            bmap[input] = i;

            bqueue.push(i);

            bpre[i][0] = 1;    //b[i][0] store thes ranking number of the girl whom this boy is asking, every boy starts from the 1st girl.

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

                scanf("%s",input);

                if(gmap[input] == 0){

                    index++;

                    gmap[input] = index;

                    gname[index] = input;

                }

                bpre[i][j] = gmap[input];

            }

        }

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

            scanf("%s", input);

            int gindex = gmap[input];

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

                scanf("%s",input);

                gpre[gindex][bmap[input]] = j; //store the rank, the smaller, the higher the propriety will be.

            }

        }

        int bindex = 0, gaskindex = 0;

        while(!bqueue.empty()){

            bindex = bqueue.front();

            //cout << "current boy's name: "<< bname[bindex] <<endl;

            bqueue.pop();

            gaskindex = bpre[bindex][bpre[bindex][0]];

            if(gres[gaskindex] < 0){

                gres[gaskindex] = bindex;

                bres[bindex] = gaskindex;

            }else if(gpre[gaskindex][bindex] < gpre[gaskindex][gres[gaskindex]]){

                bqueue.push(gres[gaskindex]);

                bpre[gres[gaskindex]][0] = (bpre[gres[gaskindex]][0]+1) % (n+1);

                gres[gaskindex] = bindex;

                bres[bindex] = gaskindex;

            }else{

                bqueue.push(bindex);

                bpre[bindex][0] = (bpre[bindex][0]+1) % (n+1);

            }

        }

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

            cout<<bname[i]<<' '<<gname[bres[i]]<<endl;

        }

    }

    return 0;

}

稳定婚姻问题,用来练习字符串输入输出。。。

猜你喜欢

转载自blog.csdn.net/qq_43439240/article/details/85497187