• Organize Your Train part II-POJ3007模拟


    Organize Your Train part II


    Time Limit: 1000MS Memory Limit: 65536K

    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
    aa
    abba
    abcd
    abcde

    Sample Output

    1
    6
    12
    18

    Source

    Japan 2006 Domestic

    思路:简单的模拟,不过不能用map来标记字符串,会超时,所以写一个哈希邻接表

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <map>
    #include <string>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int Max = 99991;
    
    typedef struct node
    {
        char str[80];
    
        node *next;
        node()
        {
            next =  0;
        }
    }Point;
    
    Point *Head[Max];
    
    int ans;
    
    bool Hash(char *s) //哈希处理冲突
    {
        int len = strlen(s);
    
        int num = 0 ;
    
        for(int i=0;i<len;i++)
        {
            num = num+s[i]*(i+1);
    
            num%=Max;
        }
    
        if(Head[num]==NULL)
        {
            Point *p = new Point;
    
            strcpy(p->str,s);
    
            Head[num]=p;
    
            return true;
        }
        else
        {
            Point *p = Head[num];
    
            while(p)
            {
                if(strcmp(p->str,s)==0)
                {
                    return false;
                }
                p=p->next;
            }
    
    
            p=new Point;
    
            strcpy(p->str,s);
    
            p->next = Head[num];
    
            Head[num] = p;
        }
    
    
        return true;
    }
    
    void Reverse(char *s,int len)//字符串翻转
    {
    
        for(int i = 0 ;i<len/2;i++)
        {
            swap(s[i],s[len-1-i]);
        }
    }
    
    void Strcat(char *s1,char *s2,char *s)
    {
        int i=0,j;
        for(j=0;s1[j]!='';j++)
        {
            s[i++]=s1[j];
        }
        for(j=0;s2[j]!='';j++)
        {
            s[i++]=s2[j];
        }
        s[i]='';
    }
    
    void Strstr(char *s,char *s1,char *s2,int mid,int len)//字符串合并
    {
        for(int i=0;i<mid;i++)
        {
            s1[i]=s[i];
        }
        s1[mid]='';
        for(int i=mid,j=0;i<len;j++,i++)
        {
            s2[j]=s[i];
        }
        s2[len-mid]='';
    }
    
    void Add(char *s1,char *s2,char *s)//判断字符
    {
        Strcat(s1,s2,s);
    
        if(Hash(s))
        {
            ans++;
        }
    
        Strcat(s2,s1,s);
    
        if(Hash(s))
        {
            ans++;
        }
    }
    
    int main()
    {
        int T;
    
        scanf("%d",&T);
    
        char s1[80],s2[80],s[80],str[80];
    
        while(T--)
        {
            scanf("%s",str);
    
            memset(Head,0,sizeof(Head));
    
            ans=0;
    
            int len=strlen(str);
    
            for(int i=1;i<=len-1;i++)
            {
                Strstr(str,s1,s2,i,len);
    
                Add(s1,s2,s);
    
                Reverse(s1,i);
    
                Add(s1,s2,s);
    
                Reverse(s2,len-i);
    
                Add(s1,s2,s);
    
                Reverse(s1,i);
    
                Add(s1,s2,s);
            }
    
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    BZOJ 2219 数论之神 (CRT推论+BSGS+原根指标)
    BZOJ 2618: [Cqoi2006]凸多边形 (半平面交)
    BZOJ 1038: [ZJOI2008]瞭望塔
    BZOJ 1007: [HNOI2008]水平可见直线 (半平面交)
    BZOJ 1845: [Cqoi2005] 三角形面积并 (辛普森积分)
    BZOJ 2458: [BeiJing2011]最小三角形 (分治)
    BZOJ 3210: 花神的浇花集会 (切比雪夫距离)
    BZOJ 2013 : [Ceoi2010]A huge tower / Luogu SP6950 CTOI10D3
    BZOJ 3630: [JLOI2014]镜面通道 (网络流 +计算几何)
    bzoj 2820 YY的GCD
  • 原文地址:https://www.cnblogs.com/juechen/p/5255881.html
Copyright © 2020-2023  润新知