举例,cheap和peach是用相同字母组成的
相同字母用不同顺序组成不同单词,看看哪些字母组合是出现最多的 =,=,
有结果版是有了最终的结果,方法一和方法二只是构建这个map的嵌套。
方式二是把map<int , map<string ,int> >从main中提出来了,单独成一个方法,本质是一样的= v=
方法二用的是打开文件夹,读取文档中的内容
最后一个是不用嵌套版,还有补充按字母表顺序排列string的方法
但是所有这些,没办法列出到底是什么词汇,所以我觉得课上教的那个版本,应该就是用map内加一个set,可以列出有什么词汇= v =
有结果版:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
typedef map<int , map<string ,int> > outsideMap;
typedef map<string ,int> insideMap;
/*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;
}
insideMap inMap(string word){
map<string, int> inside;
//string is the word in alphabetical order
inside[word] = 1;
return inside;
}
outsideMap outMap(outsideMap out,string word){
int wordSize = word.size();
/*这里是说
*比如已经有了一个key = 5
*再次出现一个单词,还是5个字母,那么只需要更新 5 对应的 insideMap*/
if(out.find(wordSize) != out.end()){
insideMap inside = out[wordSize];//创建一个map,来获取key对应的insideMap
/*下一步,在insideMap中增加一个newWord
*这里如果newWord从未在insideMap中出现,那么就相当于新添加一个pair
*如果已经出现过,那么就相当于更新一下insideMap中,string出现的频率*/
inside[word] = inside[word] + 1;
out[wordSize] = inside;//将更新后的insideMap赋值到myMap上
}else{
out[wordSize] = inMap(word);
}
return out;
}
/*这是寻找每个insideMap中出现次数的单词*/
string FindInsideMapMax(insideMap inM){
string maxString;
insideMap :: iterator insideItr = inM.begin();
int maxNum = insideItr -> second;//第一个出现的次数
maxString = insideItr -> first;
for(insideItr = inM.begin();insideItr != inM.end(); ++insideItr){
int insideItrNum = insideItr -> second;//每个出现的次数
if(insideItrNum > maxNum) {
maxNum = insideItrNum;
maxString = insideItr -> first;
}
}
return maxString;
}
int main(){
//int is the length of the word
outsideMap myMap;
ifstream in;
string txtName;
cin >> txtName;
in.open(txtName);
while(true){
string word;
in >> word;
string newWord = newSequence(word);
myMap = outMap(myMap, newWord);
if(in.eof()) break;
}
outsideMap :: iterator itr = myMap.begin();
/*outsideMap的key是单词字母的个数,它的value是insideMap
*insideMap的key是string,它的value是这个string出现的次数
从outsideMap指向insideMap的方法是
outsideMap[单词个数][string]*/
int outsideFirstKey = itr -> first;//outsideMap中第一组的key
string maxStringKey = FindInsideMapMax(myMap[outsideFirstKey]);//获取outsideMap第一组中出现频率最高的单词
int MaxNumF = (myMap[outsideFirstKey])[maxStringKey];//获取其出现的次数
for(itr = myMap.begin(); itr != myMap.end(); ++itr){
int x = itr -> first;
string StringKey = FindInsideMapMax(myMap[x]);//outsideMap中每一组的key
int stringKeyF = (myMap[x])[StringKey];//获取其出现的次数
if(stringKeyF > MaxNumF){
maxStringKey = StringKey;
MaxNumF = stringKeyF;
}
}
cout <<"由字母"<<maxStringKey <<"构成的单词出现次数最多,一共出现了" << MaxNumF << "次,它有" << maxStringKey.size()<<"个字母" << endl;
return 0;
}
方式一:
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<int , map<string ,int> > outsideMap;
typedef map<string ,int> insideMap;
/*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;
}
insideMap inMap(string word){
map<string, int> inside;
//string is the word in alphabetical order
inside[word] = 1;
return inside;
}
int main(){
//int is the length of the word
outsideMap myMap;
while(true){
string word;
cin >> word;
if(word == "0") break;
string newWord = newSequence(word);
if(myMap.find(newWord.size()) != myMap.end()){
/*这里是说
*比如已经有了一个key = 5
*再次出现一个单词,还是5个字母,那么只需要更新 5 对应的 insideMap*/
insideMap inside = myMap[newWord.size()];//创建一个map,来获取key对应的insideMap
/*下一步,在insideMap中增加一个newWord
*这里如果newWord从未在insideMap中出现,那么就相当于新添加一个pair
*如果已经出现过,那么就相当于更新一下insideMap中,string出现的频率*/
inside[newWord] = inside[newWord] + 1;
myMap[newWord.size()] = inside;//将更新后的insideMap赋值到myMap上
}else{
myMap[newWord.size()] = inMap(newWord);
}
}
outsideMap :: iterator itr;
map<string ,int> :: iterator insideItr;
for(itr = myMap.begin(); itr != myMap.end(); ++itr){
int itr1 = itr -> first;
for(insideItr = (myMap[itr1]).begin();
insideItr != (myMap[itr1]).end(); ++insideItr){
cout << itr1 << " " << insideItr -> first << " "<< insideItr -> second << endl;
}
}
return 0;
}
方式二:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
typedef map<int , map<string ,int> > outsideMap;
typedef map<string ,int> insideMap;
/*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;
}
insideMap inMap(string word){
map<string, int> inside;
//string is the word in alphabetical order
inside[word] = 1;
return inside;
}
outsideMap outMap(outsideMap out,string word){
int wordSize = word.size();
/*这里是说
*比如已经有了一个key = 5
*再次出现一个单词,还是5个字母,那么只需要更新 5 对应的 insideMap*/
if(out.find(wordSize) != out.end()){
insideMap inside = out[wordSize];//创建一个map,来获取key对应的insideMap
/*下一步,在insideMap中增加一个newWord
*这里如果newWord从未在insideMap中出现,那么就相当于新添加一个pair
*如果已经出现过,那么就相当于更新一下insideMap中,string出现的频率*/
inside[word] = inside[word] + 1;
out[wordSize] = inside;//将更新后的insideMap赋值到myMap上
}else{
out[wordSize] = inMap(word);
}
return out;
}
int main(){
//int is the length of the word
outsideMap myMap;
ifstream in;
string txtName;
cin >> txtName;
in.open(txtName);
while(true){
string word;
in >> word;
string newWord = newSequence(word);
myMap = outMap(myMap, newWord);
if(in.eof()) break;
}
outsideMap :: iterator itr;
map<string ,int> :: iterator insideItr;
for(itr = myMap.begin(); itr != myMap.end(); ++itr){
int itr1 = itr -> first;
for(insideItr = (myMap[itr1]).begin();
insideItr != (myMap[itr1]).end(); ++insideItr){
cout << itr1 << " " << insideItr -> first << " "<< insideItr -> second << endl;
}
}
return 0;
}
不用嵌套
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
typedef map<int , map<string ,int> > outsideMap;
typedef map<string ,int> insideMap;
/*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;
}
int main(){
//int is the length of the word
map<string, int> myMap;
ifstream in;
string txtName;
cin >> txtName;
in.open(txtName);
while(true){
string word;
in >> word;
string newWord = newSequence(word);
if(myMap.find(newWord) != myMap.end()){
int cout = myMap[newWord] + 1;
myMap[newWord] = cout;
}else{
myMap[newWord] = 1;
}
if(in.eof()) break;
}
map<string, int> :: iterator itr = myMap.begin();
int MaxNum = itr -> second;
string MaxString = itr -> first;
for(itr = myMap.begin(); itr != myMap.end(); ++itr){
int num = itr -> second;
if(num > MaxNum){
MaxNum = num;
MaxString = itr ->first;
}
}
cout <<"由字母"<< MaxString <<"构成的单词出现次数最多,一共出现了" << MaxNum << "次,它有" << MaxString.size()<<"个字母" << endl;
return 0;
}
排列字母
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void SwapChar(char &first, char &second){
char temp = second;
second = first;
first = temp;
}
//newSequence的第一种方法
string newSequence(string word){
for(int i = 0; i < word.size(); i++){
int minIndex = i;
for(int j = i ; j< word.size(); j++ )
if(word[j] < word[minIndex]) minIndex = j;//这个时候不发生交换,只是找到整个字符串中最小的那个字符,然后再下一步和首字符交换
SwapChar(word[i], word[minIndex] );
}
return word;
}
//newSequence的第二种方法
string newSequence2(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;
}
//newSequence的第三种方法,是不需要额外写出一个交换的
string newSequence3(string word){
string newWord = "";
char firstChar = word[0];
char nextChar;
for(int i = 0; i < word.size(); i++){
nextChar = word[i];
if(firstChar > nextChar){
newWord = nextChar + newWord;
firstChar = nextChar;
}else{
newWord = newWord + nextChar;
}
}
return newWord;
}
int main(){
while(true){
string word;
cin >> word;
if(word == "0") break;
cout << newSequence2(word) << endl;
}
return 0;
}