uva498 Simply Subsets

题目:给两个字符串,判断交并补

重点: 字符串的读取和处理,交并补的判断

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sstream>

#include<cstdio>

#include<iostream>

#include<math.h>

#include <map>

#include <vector>

#include <algorithm>//注意find不属于vector

#define M 10 //注意用define最后没有句号

using namespacestd;


int main(){

    string a,b,temp;

    vector<string> numa;

    vector<string> numb;

    vector<string> ::iterator it;//注意那两个点

    while(getline(cin,a)){

        getline(cin,b);

        istringstream record(a);  //1️⃣利用sstream。把getline读到的串用空格分开。push进去

        while(record >> temp){

            numa.push_back(temp);

        }

        istringstream recordb(b);

        while(recordb >> temp){

            numb.push_back(temp);

        }

        

        unsignedlong size = numb.size();

        for(int i =0; i < size; i ++) {

            it = find(numa.begin(), numa.end(), numb[i]);  //2️⃣find的使用方法,返回迭代器,find里面是两个迭代器(开始和结束)➕查找的元素

            if(it != numa.end()){  //通过判断迭代器是不是指到最后判断

                numb.erase(numb.begin()+i);    //3️⃣erase的使用方法也是利用迭代器

                numa.erase(it);

                i--;

            }

        }

        if((numb.empty() ==1) && (numa.empty() ==1) ) cout<<"A equals B"<<endl;

        elseif((numb.empty() ==1) && (numa.empty() ==0) )  cout<<"B is a proper subset of A"<<endl;//4️⃣如果是空,empty返回true

        elseif((numb.empty() ==0) && (numa.empty() ==1) )  cout<<"A is a proper subset of B"<<endl;

        elseif(numb.size() == size)cout<<"A and B are disjoint"<<endl;

        elsecout<<"I'm confused!"<<endl;

        numa.clear();

        numb.clear();//如果不清理干净。之前如果vector非空,会把元素继续往后加入5️⃣

    

        

    }

    return0;

}

/*

55 27

55 27

9 24 1995

9 24

1 2 3

1 2 3 4

1 2 3

4 5 6

1 2

2 3

输出:

A equals B

B is a proper subset of A

A is a proper subset of B

A and B are disjoint

I'm confused!





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值