文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>sinodragon21闯pythonchallenge第2关(C++实现)

sinodragon21闯pythonchallenge第2关(C++实现)

时间:2010-07-31  来源:sinodragon21

作者:金鸽

欢迎访问 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("http://www.pythonchallenge.com/pc/def/ocr.html")
   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)


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载