poj 3007Organize Your Train part II(哈希表 链地址解决冲突)

本文介绍了一种使用哈希表解决冲突的方法,并通过单向链表存储数据。此外,还详细描述了一个关于列车配置的问题,包括如何通过哈希表记录不同的列车配置,以及如何计算可能的列车配置数量。

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


           关键字为整数,冲突解决用单向链表

           Hash表建立函数     关键字搜素函数

解决方法:

  (1)采用除留余数法构造哈希函数,冲突解决采用链地址法。

  (2)具体的关键字列表为(19,14,23,01,68,20,84,27,55,11,10,79),则哈希函数为H(key)=key MOD 13。则采用除留余数法和链地址法后得到的预想结果应该为:

  (3)哈希造表完成后,进行查找时,首先是根据哈希函数找到关键字的位置链,然后在该链中进行搜索,如果存在和关键字值相同的值,则查找成功,否则若到链表尾部仍未找到,则该关键字不存在。

Organize Your Train part II
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8106 Accepted: 2345

Description

RJ Freight, a Japanese railroad company for freight operations has recently constructed exchange lines at Hazawa, Yokohama. The layout of the lines is shown in Figure 1.


Figure 1: Layout of the exchange lines

A freight train consists of 2 to 72 freight cars. There are 26 types of freight cars, which are denoted by 26 lowercase letters from "a" to "z". The cars of the same type are indistinguishable from each other, and each car's direction doesn't matter either. Thus, a string of lowercase letters of length 2 to 72 is sufficient to completely express the configuration of a train.

Upon arrival at the exchange lines, a train is divided into two sub-trains at an arbitrary position (prior to entering the storage lines). Each of the sub-trains may have its direction reversed (using the reversal line). Finally, the two sub-trains are connected in either order to form the final configuration. Note that the reversal operation is optional for each of the sub-trains.

For example, if the arrival configuration is "abcd", the train is split into two sub-trains of either 3:1, 2:2 or 1:3 cars. For each of the splitting, possible final configurations are as follows ("+" indicates final concatenation position):

  [3:1]
    abc+d  cba+d  d+abc  d+cba
  [2:2]
    ab+cd  ab+dc  ba+cd  ba+dc  cd+ab  cd+ba  dc+ab  dc+ba
  [1:3]
    a+bcd  a+dcb  bcd+a  dcb+a

Excluding duplicates, 12 distinct configurations are possible.

Given an arrival configuration, answer the number of distinct configurations which can be constructed using the exchange lines described above.

Input

The entire input looks like the following.

the number of datasets = m
1st dataset 
2nd dataset
 
... 
m-th dataset

Each dataset represents an arriving train, and is a string of 2 to 72 lowercase letters in an input line.

Output

For each dataset, output the number of possible train configurations in a line. No other characters should appear in the output.

Sample Input

4
#include<stdio.h>
#include<string.h>
#define mod 99991
typedef struct node
{
  char s[80];
  struct node *next;
  node()
  {
      next=0;
  }
}hashtable;

int cnt=0;
hashtable *Hash[mod];

void strcut(char *train,char *s1,char *s2,int k,int n)//将train 分成s1,s2
{
    int p=0,q=0;
    for(;p<k;p++)
        s1[p]=train[p];
    s1[p]='\0';
    for(;p<n;p++)
        s2[q++]=train[p];
    s2[q]='\0';
    return;
}
char strcat(char *s1,char *s2,char *str)//链接s1,s2
{
    int i=0,p=0;
    for(i=0;s1[i]!='\0';i++)
       str[p++]=s1[i];

       for(i=0;s2[i]!='\0';i++)
       str[p++]=s2[i];

    str[p]='\0';

}
void strres(char *s1,char *s2,int len)//将s1倒置s2
{
    int p=0;
    s2[len]='\0';
    for(int i=len-1;i>=0;i--)
        s2[i]=s1[p++];
        return ;
}
void strcopy(char *s1,char *s2)//将s1复制成s2
{
    int i,p=0;
    for(i=0;s1[i]!='\0';i++)
    {
        s2[i]=s1[i];
    }
    s2[i]='\0';
    return;
}
void hash1(char *ps)//解决哈希冲突 采用链地址法 函数为key+=s[i]*(i+1);
{
    int i;
    int key=0;
    char *s=ps;
    for(i=0;s[i]!='\0';i++)
    {
        key+=s[i]*(i+1);//哈希函数
    }
    key%=mod;
    if(!Hash[key])//关键字没有出现
    {
        hashtable *t=new hashtable;
        strcopy(s,t->s);
        Hash[key]=t;//如果没出现那么第一次出现的就是 该关键字的头地址
        cnt++;
    }
    else//出现冲突
    {
        hashtable *tt=Hash[key];
        if(!strcmp(tt->s,s)) return;
        else
        {
            while(tt->next)
            {
                if(!strcmp(tt->next->s,s))//有重合的直接返回
                    return;
                tt=tt->next;
            }
            hashtable *tmp=new hashtable;//没有出现重合就链接新的地址,并计数
            strcopy(s,tmp->s);
            tt->next=tmp;
            cnt++;
        }
    }
    return ;
}
int main()
{
    /*char s1[200],s2[200],tr[200];
    scanf("%s",s1);
    int len =strlen(tr);
    strcopy(s1,s2);
    printf("%s\n",s2);*/
    int n;
    int i,j;

    scanf("%d",&n);
    while(n--)
    {
        char train[80];
        cnt=0;
        memset(Hash,0,sizeof(Hash));
        scanf("%s",train);
        int len = strlen(train);
        if(len==1)
        {
            printf("1\n");continue;
        }
        for(i=1;i<=len-1;i++)
        {
            char s1[80],s2[80],s3[80],s4[80],s[80];
            strcut(train,s1,s2,i,len);
            strres(s1,s3,i);
            strres(s2,s4,len-i);

            strcat(s1,s2,s);
            hash1(s);
            //printf("%s\n",s);
            strcat(s2,s1,s);
            //printf("%s\n",s);
            hash1(s);
            strcat(s1,s4,s);
            hash1(s);
            //printf("%s\n",s);
            strcat(s4,s1,s);
            hash1(s);
            //printf("%s\n",s);
            strcat(s2,s3,s);
            //printf("%s\n",s);
            hash1(s);
            strcat(s3,s2,s);
            //printf("%s\n",s);
            hash1(s);
            strcat(s3,s4,s);
            hash1(s);
            strcat(s4,s3,s);
            hash1(s);

        }
        printf("%d\n",cnt);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值