Last active
November 5, 2019 08:50
-
-
Save pinglunliao/8e16d7e1d073d001a66b to your computer and use it in GitHub Desktop.
b050: 1. 集合運算 FYI: https://yunlinsong.blogspot.com/2015/03/blog-post.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iterator> | |
#include <vector> | |
#include <string> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
int main(void) | |
{ | |
int n; | |
int count = 0; | |
while( cin >> n ) | |
{ | |
count = count + 1; | |
if( n == 0 ) break; | |
vector<char> *dataSet = new vector<char>[n]; | |
for( int i = 0; i < n; i++ ) | |
{ | |
string input; | |
cin >> input; | |
for( unsigned j = 0; j < input.length(); j++ ) | |
input[j] = tolower(input[j]); | |
for( unsigned j = 0; j < input.length(); j++ ) | |
dataSet[i].push_back(input[j]); | |
sort(dataSet[i].begin(), dataSet[i].end()); | |
} | |
cout << "Test Case " << count << ":\n"; | |
for( int i = 0; i < n; i++ ) | |
{ | |
char name = 'A' + i; | |
cout << name << ": {"; | |
copy(dataSet[i].begin(), dataSet[i].end(), ostream_iterator<char>(cout, "")); | |
cout << "}" << endl; | |
} | |
// Union and intersection | |
for( int i = 0; i < n; i++ ) | |
{ | |
char setName = 'A' + i; | |
for( int j = i + 1; j < n; j++ ) | |
{ | |
char name = 'A' + j; | |
vector<char> setUnion( dataSet[i].size() + dataSet[j].size() ); | |
vector<char>::iterator iter = set_union( dataSet[i].begin(), dataSet[i].end(), | |
dataSet[j].begin(), dataSet[j].end(), setUnion.begin()); | |
cout << setName << "+" << name << ": {"; | |
copy(setUnion.begin(), iter, ostream_iterator<char>(cout, "")); | |
cout << "}" << endl; | |
vector<char> setIntersection( min(dataSet[i].size(), dataSet[j].size()) ); | |
iter = set_intersection( dataSet[i].begin(), dataSet[i].end(), | |
dataSet[j].begin(), dataSet[j].end(), setIntersection.begin()); | |
cout << setName << "*" << name << ": {"; | |
copy(setIntersection.begin(), iter, ostream_iterator<char>(cout, "")); | |
cout << "}" << endl; | |
} | |
} | |
for( int i = 0; i < n; i++ ) | |
{ | |
char setName = 'A' + i; | |
for( int j = 0; j < n; j++ ) | |
{ | |
char name = 'A' + j; | |
if( i != j ) | |
{ | |
vector<char> difference( max(dataSet[i].size(), dataSet[j].size()) ); | |
vector<char>::iterator iter = set_difference( dataSet[i].begin(), dataSet[i].end(), | |
dataSet[j].begin(), dataSet[j].end(), difference.begin()); | |
cout << setName << "-" << name << ": {"; | |
copy(difference.begin(), iter, ostream_iterator<char>(cout, "")); | |
cout << "}" << endl; | |
} | |
} | |
} | |
for( int i = 0; i < n; i++ ) | |
{ | |
char setName = 'A' + i; | |
for( int j = 0; j < n; j++ ) | |
{ | |
char name = 'A' + j; | |
if( i != j ) | |
{ | |
if(includes( dataSet[i].begin(), dataSet[i].end(), | |
dataSet[j].begin(), dataSet[j].end()) == true) | |
{ | |
cout << setName << " contains " << name << endl; | |
} | |
else | |
{ | |
cout << setName << " does not contain " << name << endl; | |
} | |
} | |
} | |
} | |
delete [] dataSet; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment