算法初步——哈希表B1043输出PATest

那年仲夏 提交于 2020-01-07 14:52:06

 

 

#include <bits/stdc++.h>
#include<math.h>
#include <string>
using namespace std;
const int MAX_LEN = 10005;
char str[MAX_LEN];
int hashTable[30] = {0};
int main(){
    cin>>str;
    int len = strlen(str);
    int count = 0;
    //map<char,int> result;
    for(int i =0;i<len;++i){
        if(str[i] == 'P' || str[i] == 'A' || str[i] == 'T' || str[i] == 'e' || str[i] == 's' || str[i] == 't'){
            count++;
        }
    }
    map<char,int> result;
    //vector<char> result[count];
    for(int i =0;i<len;++i){
        if(str[i] == 'P' || str[i] == 'A' || str[i] == 'T' || str[i] == 'e' || str[i] == 's' || str[i] == 't'){
            result[str[i]]++;
        }
    }
    map<char,int>::iterator iter;
    /*while(count != 0){
        
    }*/
    /*for(iter = result.begin();iter != result.end();iter++){
        if(iter->)
    }*/
    for(int i =0;i<count;++i){
        //map<char,int>::iterator iter;
        iter = result.begin();
        for(;iter != result.end();++iter){
            if(iter->first == 'P' && iter->second > 0){
                cout<<iter->first;
                iter->second--;
                break;
            }
        }
        iter = result.begin();
        for(;iter != result.end();++iter){
            if(iter->first == 'A' && iter->second > 0){
                cout<<iter->first;
                iter->second--;
                break;
            }
        }
        iter = result.begin();
        for(;iter != result.end();++iter){
            if(iter->first == 'T' && iter->second > 0){
                cout<<iter->first;
                iter->second--;
                break;
            }
        }
        iter = result.begin();
        for(;iter != result.end();++iter){
            if(iter->first == 'e' && iter->second > 0){
                cout<<iter->first;
                iter->second--;
                break;
            }
        }
        iter = result.begin();
        for(;iter != result.end();++iter){
            if(iter->first == 's' && iter->second > 0){
                cout<<iter->first;
                iter->second--;
                break;
            }
        }
        iter = result.begin();
        for(;iter != result.end();++iter){
            if(iter->first == 't' && iter->second > 0){
                cout<<iter->first;
                iter->second--;
                break;
            }
        }

    }
    system("pause");
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!