1.首先需要找打大于600000的最大素数,寻找素数的方法,我用的是比较笨的方法,就是用x的平方根以下的数y去除,若x%y全部都不为0,则为素数。
2.对每一个菜谱进行编码,我的编码方法如下,具体的编码方法大家可以随意!
int hashcode(char be_hash[40])
{
int length= strlen(be_hash);
int num = 0;
for (auto i = 0; i < length; i++)
{
num += (be_hash[i] - 96)*(i+1);
}
return num;
}
3.每输入一个菜谱,进行一次检查,检查的结果为result。
若从来没有输入,则result.bool_value=1;然后将值添加入链表数组中。
(这里需要注意的是,如果hashcode计算得到的值相同,则需要遍历链表以判断是否重复)
4.在检查时,会使result.chongfu++,然后通过这个来判断重复了几次,若重复了超过1次,chongfu的值会大于1.这时候重复的数值就不能加入rept_hash链表中。
5.最后遍历rept_hash链表,输出即可!
最后附上代码!
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#define maxn 799999
using namespace std;
struct result {
int bool_value;
int chongfu;
};
//节点是指节点内容
struct listnode{
listnode*pre, *succ;
char*meal_name;
int chongfu = -1;
listnode(){}
listnode(char*a,listnode*p1=NULL,listnode*p2=NULL):meal_name(a),pre(p1),succ(p2){}
};
//列表是包括多个节点的组合
int count = 0;
struct list {
listnode*head, *tail;
list() { init(); }
void init()
{
head = new listnode;
tail = new listnode;
head->pre = NULL; head->succ = tail;
tail->pre = head; tail->succ = NULL;
}
//每一个新节点都插入到head之后
void insert_head(char*mealnm)
{
auto p = new listnode(mealnm);
p->succ = head->succ;
head->succ->pre = p;
p->pre = head;
head->succ = p;
}
void insert_tail(char*mealnm)
{
auto p = new listnode(mealnm);
p->pre = tail->pre;
tail->pre->succ = p;
p->succ = tail;
tail->pre = p;
}
result check_rpt1(char*mealnm);
};
char meal[600000][41];
list list_hash[maxn];
list rept_hash;
result list::check_rpt1(char*mealnm)
{
auto p = this->head;
result t;
while ((p=p->succ) != this->tail)
{
if (strcmp(p->meal_name, mealnm)==0)
{
t.bool_value = 0;
t.chongfu=++p->chongfu;
return t;
}
}
t.bool_value = 1;
t.chongfu = 0;
return t;
}
int hashcode(char be_hash[40])
{
int length= strlen(be_hash);
int num = 0;
for (auto i = 0; i < length; i++)
{
num += (be_hash[i] - 96)*(i+1);
}
return num;
}
int main()
{
#ifndef _OJ_
freopen("reptmealin.txt", "r", stdin);
#endif
int n,num,list_location,rept_num=0;
scanf("%d", &n);
for (auto i = 0; i < n; i++)
{
scanf("%s", meal[i]);
num = hashcode(meal[i]);
list_location = num % maxn;
int lenght = strlen(meal[i]);
auto reslt = list_hash[list_location].check_rpt1(meal[i]);
if (reslt.bool_value)
{
list_hash[list_location].insert_head(meal[i]);
//printf("\n");
}
else if (reslt.chongfu<1)
rept_hash.insert_tail(meal[i]);
else;
}
auto p = rept_hash.head;
while ((p = p->succ) != rept_hash.tail)
{
printf("%s\n", p->meal_name);
}
}