• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

  • 联系
  • 订阅
  • 管理

View Post

Sicily 1194. Message Flood 排序+二分

       这题本来很容易,由于自己看漏了个条件(大小写不敏感)WA了无数,还有用错了stl里的sort(参数传错了)

       用stl里的sort排数组时, sort(begin, end, cmp),第二个参数要是数组最后元素的后一个的指针,当一个数组a有n个元素时,end= a+n(&a[n]),而不是&a[n-1]

#include <iostream>
#include <string>
#include <algorithm>
#include <memory.h>
using namespace std;

bool cmp(string a, string b)
{
	return a < b;
}

string name[20001], tmp;
bool found[20001];
int n, m;
bool bi_search();

int main()
{
	int count;

	while (cin >> n && n)
	{
		cin >> m;
		memset(found, false, 20001*sizeof(bool));
		for (int i = 0; i < n; i++)
		{
			cin >> name[i];
			for (int j = 0; j < name[i].length(); j++)
				name[i][j] = tolower(name[i][j]);
		}
		count = n;
		sort(name, name+n, cmp);
		for (int i = 0; i < m; i++)
		{
			cin >> tmp;
			for (int j = 0; j < tmp.length(); j++)
				tmp[j] = tolower(tmp[j]);

			if (bi_search())
				count--;
		}
		cout << count << endl;
	}
	return 0;
}

bool bi_search()
{
	int start = 0, mid, end = n-1;
	while (start <= end)
	{
		mid = (start+end)/2;

		if (tmp == name[mid])
		{
			if (!found[mid])
			{
				found[mid] = true;
				return true;
			}
			else
				return false;
		}
		else if (tmp < name[mid])
			end = mid-1;
		else 
			start = mid+1;
	}
	return false;
}

posted on 2010-11-13 16:37  sysuwhj  阅读(1379)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3