Poj 1583 Choose Your Words Carefully + Poj 1119 Start Up the Startup (字符串 map)
来源:互联网 发布:云计算基础架构 编辑:程序博客网 时间:2024/06/10 02:35
几天没敲代码,感觉手有点生了。。。渐渐进入考研复习的节奏,不知还能不能保持训练的时间。
本来想练字符串Hash的,不知不觉就敲成了map,STL已经戒不掉了。。。
isdigit islower isupper tolower 这几个函数还是挺好用的,据说实现用的是宏定义?
Poj 1583 Choose Your Words Carefully
#include <iostream>#include <cstring>#include <string>#include <map>using namespace std;#define upmax(a,b) ((a)=(a)>(b)?(a):(b))int main (){int limit=0,tmp;string str;map<string, int> mp;while (cin>>str){int len=str.length();string ch;bool flag=false; for (int i=0;i<len;i++)if (isalpha(str[i])){str[i] = tolower(str[i]); //ch=ch+str[i];flag = false; }else{tmp = ++mp[ch];flag = true;upmax(limit,tmp);ch="";} if (flag==false){tmp=++mp[ch];upmax(limit,tmp);}}printf("%d occurrences\n",limit);map<string, int>::iterator it;for (it=mp.begin();it!=mp.end();it++)if (it->second == limit)cout << it->first << endl;return 0;}
Poj 1119 Start Up the Startup
参考了:http://hi.baidu.com/zhuangxie1013/item/3e3c2374a47e19560c0a07f7
#include <iostream>#include <string>#include <map>#include <cmath>#include <algorithm>using namespace std;string ten="----------";int main (){int i;double m1,m2,num;string source; //原串string after; //去掉符号,保留数字,所有字母小写map<string,int> match; //搜索项在搜索字符串中出现的次数map<string,int> doc; //搜索项在文档中出现的次数map<string,int>::iterator it;while (cin>>source) //统计搜索项在搜索字符串中出现的次数{if (source==ten) break;after="";for (i=0;i<source.size();i++){if (isdigit(source[i]) || islower(source[i]))after+=source[i];else if (isupper(source[i]))after+=(tolower(source[i]));}if (after!="")match[after]++;}bool docEnd=false;while (cin>>source)if (docEnd==true && source==ten) break;else if (docEnd==false && source==ten){docEnd=true;num=0.0;for (it=match.begin();it!=match.end();it++){m1=it->second; //搜索项在搜索字符串中出现的次数m2=doc[it->first]; //搜索项在文档中出现的次数num+=sqrt(m1*m2); //计算文档的分值}printf("%.2lf\n",num);doc.clear(); //清空该文档的所有搜索项}else //统计搜索项在文档中出现的次数{docEnd=false;after="";for (i=0;i<source.size();i++){if (isdigit(source[i])||islower(source[i]))after+=source[i];else if(isupper(source[i]))after+=(source[i]-'A'+'a');}if (match[after]>0)doc[after]++;}return 0;}
0 0
- Poj 1583 Choose Your Words Carefully + Poj 1119 Start Up the Startup (字符串 map)
- poj 1583 Choose Your Words Carefully
- POJ 1583 Choose Your Words Carefully 笔记
- POJ 1119 Start Up the Startup
- POJ 1119 Start Up the Startup 已被翻译
- Hoj 1307 Choose Your Words Carefully、Hoj 1989 The Most Frequent Word
- Choose Your Weapons Carefully, Relinquish Them Reluctantly
- zoj 1050 Start Up the Startup
- ZOJ 1050 start up the startup
- poj 2023 Choose Your Own Adventure
- Poj 2023 Choose Your Own Adventure
- poj 2023 Choose Your Own Adventure 树形结构的搜索
- POJ 3087-Shuffle'm Up(map+模拟)
- Effective STL 24 Choose carefully between map::operator[] and map::insert
- Words of the Start
- poj 字符串相关之1816 Wild Words
- The start—— POJ 1011 Sticks
- POJ 3087 shuffle'em up map判重
- asp.net远程调试
- Custom tool error Failed to generate code
- 库与框架
- 吐槽我遇见的奇葩办事经历
- MVC3.0 中Razor 学习 cshtml文件
- Poj 1583 Choose Your Words Carefully + Poj 1119 Start Up the Startup (字符串 map)
- 面向对象与原型【36】--原型字面量写法
- CareerCup Add two number
- Java 中访问修饰符public,private,protected和default(friendly) 的区别
- Oracle 11g 数据库详细安装说明
- Recently Viewed Products 修改
- 算法实验 学生管理系统 线性表实现
- 第22条:优先使用静态成员类
- java集合框架--List、Set、Map