SDUT1500_Message Flood(字典树)

2024-09-04 08:58
文章标签 message 字典 flood sdut1500

本文主要是介绍SDUT1500_Message Flood(字典树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Message Flood

Time Limit: 1500MS Memory limit: 65536K

题目描述

Well, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot". However, If you ask Merlin this question on the New Year's Eve, he will definitely answer "What a trouble! I have to keep my fingers moving on the phone the whole night, because I have so many greeting message to send!" Yes, Merlin has such a long name list of his friends, and he would like to send a greeting message to each of them. What's worse, Merlin has another long name list of senders that have sent message to him, and he doesn't want to send another message to bother them Merlin is so polite that he always replies each message he receives immediately). So, before he begins to send message, he needs to figure to how many friends are left to be sent. Please write a program to help him. Here is something that you should note. First, Merlin's friend list is not ordered, and each name is alphabetic strings and case insensitive. These names are guaranteed to be not duplicated. Second, some senders may send more than one message to Merlin, therefore the sender list may be duplicated. Third, Merlin is known by so many people, that's why some message senders are even not included in his friend list.

输入

There are multiple test cases. In each case, at the first line there are two numbers n and m (1<=n,m<=20000), which is the number of friends and the number of messages he has received. And then there are n lines of alphabetic strings(the length of each will be less than 10), indicating the names of Merlin's friends, one per line. After that there are m lines of alphabetic strings, which are the names of message senders. The input is terminated by n=0.

输出

For each case, print one integer in one line which indicates the number of left friends he must send.

示例输入

5 3
Inkfish
Henry
Carp
Max
Jericho
Carp
Max
Carp
0

示例输出

3

来源

第9届中山大学程序设计竞赛预选赛

解题报告
这题在当初学字符串的时候做的,又是英文题,题意大体是一个人手上有一个名单,在新年那一天他需要给名单上的人发短信,然而有小伙伴先给他发短信了(可以发多次信息),他一收到短信就回复小伙伴,问题是除去给他发信息的小伙伴,他还需要给几个人发信息。
一开始就以为是模拟的题目,直接敲代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void *p1,const void *p2)//对于char类型的排序
{//降序排序//return strcmp((char *)p2,(char *)p1);//升序排序return strcmp((char *)p1,(char *)p2);
}
char name[20020][100],send[20020][100];
int main()
{int n,m;int i,j;while(scanf("%d",&n)!=EOF&&n){scanf("%d%*c",&m);int c=0,l=0;for(i=0;i<n;i++){scanf("%s",name[i]);}for(i=0;i<m;i++){scanf("%s",send[i]);}qsort(send,m,sizeof(send[0]),cmp);j=0;for(i=0;i<m;i++){if(strcmp(send[i],send[i+1])!=0){strcpy(send[j++],send[i]);l++;}}for(i=0;i<l;i++)for(j=0;j<n;j++){if(strcmp(name[j],send[i])==0)c++;}printf("%d\n",n-c);}
}
/**************************************Problem id	: SDUT OJ 1500User name	: acm2013叶思俊Result		: Time Limit ExceededTake Memory	: 0KTake Time	: 1510MSSubmit Time	: 2014-02-10 15:12:17
**************************************/
这题给的时间是1500ms,两个名单都是20000- 的,双重循环都4亿次的节奏,还用到快排。。。超时。。。
还用了链表写了次。。。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{char name[100];node *next;
};
int main()
{int n,m;int i,j;while(scanf("%d",&n)!=EOF&&n){int c=0;scanf("%d%*c",&m);node *head1=new node;head1->next=NULL;node *head2=new node;head2->next=NULL;node *tail1=head1,*tail2=head2;for(i=0;i<n;i++){node *p=new node;scanf("%s",p->name);p->next=NULL;tail1->next=p;tail1=p;}for(i=0;i<m;i++){node *p=new node;scanf("%s",p->name);p->next=NULL;tail2->next=p;tail2=p;}node *q=head2->next;while(q!=NULL){node *p=head1;while(p->next!=NULL){if(strcmp(q->name,p->next->name)==0){p->next=p->next->next;c++;}else{p=p->next;}}q=q->next;}printf("%d\n",n-c);}
}

同样超时。。。今天学了字典树,是个好东西。。。
要注意的是以哪个名单建字典树。。。
以名单一的建树,你对名单二查找时还要注意重复名字的。。。
所以以名单二建字典树。。。
#include <stdio.h>
#include <string.h>
#include <algorithm>struct node
{int v;node *next[26];
};
node *newnode()
{node *p=new node;p->v=0;for(int i=0;i<26;i++){p->next[i]=NULL;}return p;
}
void insertnode(node *root,char *str)
{node *p=root;int l=strlen(str);for(int i=0;i<l;i++){int t;if(str[i]>='a')t=str[i]-'a';else t=str[i]-'A';if(p->next[t]==NULL){p->next[t]=newnode();}p=p->next[t];}p->v=1;
}
int find(node *root,char *str)
{node *p=root;int l=strlen(str);for(int i=0;i<l;i++){int t;if(str[i]>='a')t=str[i]-'a';else t=str[i]-'A';if(p->next[t]==NULL)return 0;else p=p->next[t];}if(p->v==1)return 1;return 0;
}
int main()
{int n,m;int i,j,k;char name[30000][15],send[100000];while(~scanf("%d",&n)&&n){int c=0;node *root=newnode();scanf("%d%*c",&m);for(i=0;i<n;i++){scanf("%s",name[i]);}for(i=0;i<m;i++){scanf("%s",send);insertnode(root,send);}for(i=0;i<n;i++){if(!find(root,name[i]))c++;}printf("%d\n",c);}
}



这篇关于SDUT1500_Message Flood(字典树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1135568

相关文章

POJ2001字典树

给出n个单词,求出每个单词的非公共前缀,如果没有,则输出自己。 import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;

python 实现第k个字典排列算法

第k个字典排列算法介绍 "第k个字典排列"算法通常指的是在给定的字符集合(例如,字符串中的字符)中,找到所有可能排列的第k个排列。这个问题可以通过多种方法解决,但一个常见且高效的方法是使用“下一个排列”算法的变种,或称为“第k个排列”的直接算法。 方法一:使用“下一个排列”的变种 生成所有排列:首先生成所有排列,但显然这种方法对于较大的输入集合是不切实际的,因为它涉及到大量的计算和存储。 排序

POJ3617(字典序最小问题)

书中43页 此题有坑点,PE了40分钟.也是醉了....题目要求每80个字符才换行,而且最后一个如果恰好就不用换,这不是无聊嘛....... #include <iostream>#include <cstdio>#include <cstring>using namespace std;int n,m;char S[2100],P[2100];int main(){#ifd

POJ 2001 Shortest Prefixes(字典树入门)

题目: http://poj.org/problem?id=2001 题意: 找到多个字符串中,各自唯一的最短子串,例如 carte 与 carce 各自唯一的最短子串为cart与carc,即不会与其它字符串的子串重复。 思路: 字典树 解题心得: 更新关键值的时机很重要 代码: #include<stdio.h>#include<string>typedef str

Oracle数据库(数据字典、表空间、表的创建、视图)

知识点引用: http://www.2cto.com/database/201207/142874.html http://blog.csdn.net/haiross/article/details/11772847 一. 彻底卸载Oracle 方式1、重装操作系统 方式2、 2.1 DBCA删除数据库开始 → 程序 → Oracle → 开发与移植工具 → Datab

Python的字符串,list,tuple,set,字典操作详解

1.字符串python是要创建成字符串的元素,其中的每个字母都是单一的子串,把它放在' '单引号或是'' ''引号中,就完成了python 字符串的创建。#str强制转换>>> a=123>>> b=str(a) #将整数转化为字符串>>> b'123'>>> a=[1,2,3]>>> b=str(a) #将list转化为字符串>>> b'[1, 2, 3]'#字符串下

OC中数组、字典、集合常用方法的运用

/* ====================== 一 NSArray========================          1.创建对象          1.1初始化方法(2) //一般程序有问题先检查初始化          1.2类方法          1.3字面量方法          2.数组查找          2.1通过下标访问对象[ .[i]]

python pickle 模块用于保存python内存数据(包括实例对象、字典、列表等所有python中的数据)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言基本用法 前言 Python 的 pickle 模块用于序列化和反序列化 Python 对象。这意味着你可以将 Python 对象(如列表、字典、类实例等)转换为字节流(序列化),并将其保存到文件中或在网络上传输,然后在需要的时候将其恢复为原始 Python 对象(反序列化)。 常见用途

Python 从入门到实战8(字典)

我们的目标是:通过这一套资料学习下来,通过熟练掌握python基础,然后结合经典实例、实践相结合,使我们完全掌握python,并做到独立完成项目开发的能力。       上篇文章我们通过举例学习了python 中元组的定义及相关操作。今天详细讲述字典的定义及相关的操作,也是经常使用到的。 1、字典的定义 字典是由{}括住的,内容以“键-值对”的形式存储的无序序列。 字典的主要特点如下:

Python 字典详解

Python 字典 : 字典是另一种可变容器模型,且可存储任意类型对象。 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: d = {key1 : value1, key2 : value2 } 键必须是唯一的,但值则不必。 值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。 一个简单的