Poj 1583 Choose Your Words Carefully + Poj 1119 Start Up the Startup (字符串 map)

本文提供了两道POJ题目(Poj1583和Poj1119)的C++代码解决方案,重点介绍了如何使用STL中的map进行字符串处理,并通过实例展示了字符串操作函数如tolower和isdigit的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

几天没敲代码,感觉手有点生了。。。渐渐进入考研复习的节奏,不知还能不能保持训练的时间。

本来想练字符串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;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值