版本一大概是按照课上来的,版本二是添加了一个处理同时出现多个最大的情况
版本一:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
using namespace std;
/*SwapChar and newSequenc are used to get a new string whose chars are put in alphabetical order.
使字符串中的字符按照字母表顺序排列= v=
*/
void SwapChar(char &first, char &second){
char temp = second;
second = first;
first = temp;
}
string newSequence(string word){
for(int i = 0; i < word.size(); i++){
for(int j = i + 1 ; j< word.size(); j++ )
if(word[j] < word[i]) {
SwapChar(word[j], word[i] );
}
}
return word;
}
//打印分别有什么单词
void theMaxWord(set<string> s){
set<string> :: iterator it;
cout<<"他们分别是:" ;
for(it = s.begin(); it != s.end(); ++it){
cout << *it << endl;
}
}
void readFile(ifstream &in, map<string, set<string> > &m){
while(true){
string word;
in >> word;
if(in.fail())break;
string newWord = newSequence(word);
m[newWord].insert(word);//这就相当于在map里面增加新的pair了
if(in.eof()) break;
}
int maxSize = 0;
string maxKey;
int theNextMax;
string theNextMxString;
cout << "一共有" << m.size() << "组单词" << endl;
map<string, set<string> > :: iterator itr;
for(itr = m.begin(); itr != m.end(); ++itr){
string key = itr -> first;
cout << "有" << key << "字母的单词一共" << m[key].size() << "个"<< endl;
if(m[key].size() > maxSize){
maxSize = m[key].size();
maxKey = key;
}
if(m[key].size() == maxSize){
theNextMax = m[key].size();
theNextMxString = key;
}
}
cout << "出现最多的字母顺序为" << maxKey << endl;
theMaxWord(m[maxKey]);
cout << "字母长度为" << maxKey.size()<< endl;
if(theNextMxString != ""){
cout << "出现最多的字母顺序同时还有" << theNextMxString << endl;
theMaxWord(m[theNextMxString]);
cout << "字母长度为" << theNextMxString.size()<< endl;
}
}
int main(){
//int is the length of the word
map<string, set<string> > m;
ifstream in;
string txtName;
cin >> txtName;
in.open(txtName);
readFile(in, m);
return 0;
}
版本二:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
using namespace std;
/*SwapChar and newSequenc are used to get a new string whose chars are put in alphabetical order.
使字符串中的字符按照字母表顺序排列= v=
*/
void SwapChar(char &first, char &second){
char temp = second;
second = first;
first = temp;
}
string newSequence(string word){
for(int i = 0; i < word.size(); i++){
for(int j = i + 1 ; j< word.size(); j++ )
if(word[j] < word[i]) {
SwapChar(word[j], word[i] );
}
}
return word;
}
bool findtheNext(int a, int b){
bool findnext;
if(a <= b){
findnext = true;
}else{
findnext = false;
}
return findnext;
}
//打印分别有什么单词
void theMaxWord(set<string> s){
set<string> :: iterator it;
cout<<"他们分别是:" ;
for(it = s.begin(); it != s.end(); ++it){
cout << *it <<" ";
}
}
//theNextInt和theNextWord是用来寻找下一个最大值的
int theNextInt(map<string, set<string> > m, int maxSize, string maxKey){
m.erase(maxKey);
int maxS = 0;
string maxK;
map<string, set<string> > :: iterator itr;
for(itr = m.begin(); itr != m.end(); ++itr){
string key = itr -> first;
if(m[key].size() > maxS){
maxS = m[key].size();
maxK = key;
}
}
return maxS;
}
string theNextWord(map<string, set<string> > m, int maxSize, string maxKey){
m.erase(maxKey);
int maxS = 0;
string maxK = maxKey;
map<string, set<string> > :: iterator itr;
for(itr = m.begin(); itr != m.end(); ++itr){
string key = itr -> first;
if(m[key].size() > maxS){
maxS = m[key].size();
maxK = key;
}
}
return maxK;
}
//删除副本map中的已打印的最大值
void eraseTheMax(map<string, set<string> > &m, string maxKey){
m.erase(maxKey);
}
void readFile(ifstream &in, map<string, set<string> > &m){
while(true){
string word;
in >> word;
if(in.fail())break;
string newWord = newSequence(word);
m[newWord].insert(word);//这就相当于在map里面增加新的pair了
if(in.eof()) break;
}
int maxSize = 0;
string maxKey;
cout << "一共有" << m.size() << "组单词" << endl;
map<string, set<string> > :: iterator itr;
for(itr = m.begin(); itr != m.end(); ++itr){
string key = itr -> first;
cout << "有" << key << "字母的单词一共" << m[key].size() << "个"<< endl;
if(m[key].size() > maxSize){
maxSize = m[key].size();
maxKey = key;
}
}
cout << "出现最多的字母顺序为" << maxKey << endl;
theMaxWord(m[maxKey]);
cout << "字母长度为" << maxKey.size()<< endl;
map<string, set<string> > copyM = m;
eraseTheMax(copyM, maxKey);
int Time = 1;
while(findtheNext(maxSize, theNextInt(copyM, maxSize, maxKey))){
string theNext = theNextWord(copyM, maxSize, maxKey);
int theNextNum = theNextInt(copyM, maxSize, maxKey);
cout << "出现最多的字母顺序同时还有" << theNext << endl;
theMaxWord(copyM[theNext]);
cout << "字母长度为" << theNext.size()<< endl;
eraseTheMax(copyM, theNext);
Time++;
}
cout << "一共有" << Time << "个最大值" << endl;
}
int main(){
//int is the length of the word
map<string, set<string> > m;
ifstream in;
string txtName;
cin >> txtName;
in.open(txtName);
readFile(in, m);
return 0;
}