几天没敲代码,感觉手有点生了。。。渐渐进入考研复习的节奏,不知还能不能保持训练的时间。
本来想练字符串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;
}