Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5142660
  • 博文数量: 1696
  • 博客积分: 10870
  • 博客等级: 上将
  • 技术积分: 18357
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-30 15:16
文章分类
文章存档

2017年(1)

2016年(1)

2015年(1)

2013年(1)

2012年(43)

2011年(17)

2010年(828)

2009年(568)

2008年(185)

2007年(51)

分类: C/C++

2010-07-31 20:12:36

作者:金鸽

欢迎访问 sinodragon21.cublog.cn


源代码如下:
 

#include<fstream>
#include<iostream>
#include<map>

using namespace std;

int main()
{
    ifstream ifs;
    ifs.open("input.txt");

    map<char, int> datamap;
    map<char,int>::iterator iter;

    char ch = '\0';
    
    while(!ifs.eof())
    {
        //cout << ch;

        ifs.get(ch);
        if('\n' == ch) continue;
        if(datamap.end() != datamap.find(ch))
        {
            datamap[ch] ++;
        }
        else
        {
            datamap.insert(pair<char, int>(ch, 1));
        }
    }

    int mincnt = 65535;
    char minkey = '\n';
   
    for(iter = datamap.begin(); iter != datamap.end(); iter++)
    {
        cout << "key = " << iter->first << "; cnt = " << iter->second << endl;
        if(mincnt > iter->second)
        {
            minkey = iter->first;
            mincnt = iter->second;
        }
    }


    cout << endl << "=========================" << endl;

    cout << "rare character = " << minkey << "; cnt = " << mincnt << endl;

    ifs.close();

    system("PAUSE");

    return 0;
}


待解决的问题:
1、这种排序得到的'最小出现次数'字符只能是一个,如果有多个出现频率相同的字符,就有问题了。
2、输出结果:

key = !; cnt = 6079
key = #; cnt = 6115
key = $; cnt = 6046
key = %; cnt = 6104
key = &; cnt = 6043
key = (; cnt = 6154
key = ); cnt = 6186
key = *; cnt = 6035
key = +; cnt = 6066
key = @; cnt = 6157
key = [; cnt = 6108
key = ]; cnt = 6152
key = ^; cnt = 6030
key = _; cnt = 6112
key = a; cnt = 1
key = e; cnt = 1
key = i; cnt = 1
key = l; cnt = 1
key = q; cnt = 1
key = t; cnt = 1
key = u; cnt = 1
key = y; cnt = 1
key = {; cnt = 6046
key = }; cnt = 6105

=========================
rare character = a; cnt = 1
请按任意键继续. . .


结果中 “a, e, i, l, q, t, u, y” 出现次数最少,
可如何列出这8个字符的所有排列组合?从中找到有意义的 字符串?

附:google到的Python解决方案

   1. import re
   2. import urllib
   3. import string
   4.
   5. # 使用urllib模块读取页面源代码
   6. sock = urllib.urlopen("")
   7. source = sock.read()
   8. sock.close()
   9.
  10. # 标志re.S表示在正则表达式中点(.)可以匹配任意字符,包括换行符
  11. data = re.findall(r'', source, re.S)
  12. charList = re.findall(r'([a-zA-Z])', data[1], 16)
  13.
  14. # 使用string模块将list转为字符串打印
  15. print string.join(charList)


附:google到的permutation of string in C++

// This program finds permutations using a recursive method

// modified Dev C++ from a wonderful article at:

//


#include<iostream>
#include<cstring>

using namespace std;

void char_permutation(char str[],char append[])
{
    int length = strlen(str);
    if (length)
    {
        for(int i=0;i<length;++i)
        {
            char* str1 = new char[length+1];
            int cnt;
            int cnt2;
            for(cnt=0,cnt2=0; cnt<length; ++cnt,++cnt2)
            {
                if (cnt == i)
                {
                    str1[cnt] = str[++cnt2];
                    continue;
                }
                else
                    str1[cnt] = str[cnt2];
            }
            str1[cnt] = '\0';

            int alength = strlen(append);
            char* append1 = new char [alength+2];
            strncpy(append1,append,alength);
            append1[alength] = str[i];
            append1[alength+1] = '\0';

            char_permutation(str1,append1);

            delete []str1;
            delete []append1;
        }
    }
    else
    {
        cout << append << endl;
    }
}


int main()
{
    char str[] = "aeilqtuy"; // shows a little humor

    char append[] = "\0";

    cout << "Original = " << str << endl;
    char_permutation(str,append);
    cout << "Done ........" << endl;

    cin.get(); // wait

    return 0;
}


使用C++的STL算法

// permutations

// Dev C++


#include <iostream>
#include <algorithm> // next_permutation() via stl_algo.h

#include <vector> // stl vector header

#include <iterator> // ostream_iterator

#include <stdlib.h> // system()


using namespace std;

int main()
{
    vector<char> iV; // char型可以替换为其他类型 比如int


    iV.push_back('a');
    iV.push_back('e');
    iV.push_back('i');
    iV.push_back('l');
    iV.push_back('q');
    iV.push_back('t');
    iV.push_back('u');
    iV.push_back('y');

    // display original

    cout << "original number:\n";
    copy(iV.begin(),iV.end(),ostream_iterator<char>(cout,"")); // char型可以替换为其他类型 比如int

    cout << endl;
    cout << "permutations:\n";
    // loop until all permutations are generated and displayed

    // does not display original number

    while (next_permutation(iV.begin(), iV.end()))
    {
        copy(iV.begin(),iV.end(),ostream_iterator<char>(cout,"")); // char型可以替换为其他类型 比如int

        cout << endl;
    }

    system("PAUSE");    
    return 0;
}


阅读(1052) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~