• POJ 3007 Organize Your Train part II(哈希链地址法)


    http://poj.org/problem?id=3007

    题意 :给你一个字符串,让你无论从什么地方分割,把这个字符串分成两部分s1和s2,然后再求出s3和s4,让你进行组合,看能出来多少种不同的形式。

    思路 :记得以前的时候就听他们讨论这道题,说是用map做会超时,所以我直接就没用map。。。。但是做这道题实在是太波折了,昨天晚上改了一晚上都不对,就是不知道哪里出了问题,今早上又改,改来改去才知道原来我new node后边缺了个括号,我很懵懂,我记得不用括号也行啊,而且我看到一个AC的代码就是没用括号,但是不加就不对,终于用了链地址法改对了能运行了,交上去因为数组开小了就越界了............然后又因为忘了删掉我在代码中加的测试的输出,所以Output Limit Exceeded了一次.............

    这个题主要是处理好冲突就行了。

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <stdlib.h>
    #include <string>
    using namespace std ;
    
    struct node
    {
        char ch1[1100] ;
        struct node *next ;
    }*hash[1000000] ;
    
    int cnt ;
    
    void hashh(char *ch,char *sh)
    {
        char str[1200] ;
        strcpy(str,ch) ;
        strcat(str,sh) ;
        int len = strlen(str) ;
        int sum = 0 ;
       for(int i = 0 ; i < len ; i++)
            sum += (str[i]*i)  ;
        if(!hash[sum])
        {
            node *p= new node() ;
            strcpy(p->ch1,str) ;
            hash[sum] = p ;
            cnt++ ;
        }
        else
        {
            node *p = hash[sum] ;
            if(!strcmp(p->ch1,str)) return ;
            else
            {
                while(p->next)
                {
                    if(!strcmp(p->next->ch1,str)) return ;
                    p = p->next ;
                }
                node *q = new node() ;
                strcpy(q->ch1,str) ;
                p->next = q ;
                cnt++ ;
            }
        }
        return ;
    }
    int main()
    {
        int n ;
        scanf("%d",&n) ;
        char str[1100],str1[1010],str2[1100],str3[1010],str4[1010]  ;
        while(n--)
        {
            cnt = 0 ;
            memset(hash,0,sizeof(hash)) ;
            scanf("%s",str) ;
            int len = strlen(str) ;
            for(int i = 1 ; i < len ; i++)
            {
                int j = 0 , k = 0 ;
                for( ; j < i ; j++)
                    str1[j] = str[j] ;
                str1[j] = '' ;
                for(j = i ; j < len ; j++)
                    str2[k++] = str[j] ;
                str2[k] = '' ;
                strcpy(str3,str1) ;
                strcpy(str4,str2) ;
                reverse(str3,str3+i) ;
                reverse(str4,str4+k) ;
              //  printf("%s %s %s %s
    ",str1,str2,str3,str4) ;
                hashh(str1,str2) ;
                hashh(str3,str2) ;
                hashh(str1,str4) ;
                hashh(str3,str4) ;
                hashh(str2,str1) ;
                hashh(str2,str3) ;
                hashh(str4,str1) ;
                hashh(str4,str3) ;
            }
            printf("%d
    ",cnt) ;
        }
        return 0 ;
    }
    View Code
  • 相关阅读:
    [Swift]LeetCode1093. 大样本统计 | Statistics from a Large Sample
    [Swift]鸡尾酒排序 | Cocktail Sort
    [Swift]插入排序 | Insertion sort
    [Swift]求最大公约数和最小公倍数
    [Swift]LeetCode1087. 字母切换 | Permutation of Letters
    [Swift]LeetCode1086. 前五科的均分 | High Five
    [Swift]LeetCode1088. 易混淆数 II | Confusing Number II
    [Swift]LeetCode1090. 受标签影响的最大值 | Largest Values From Labels
    [Swift]LeetCode1091. 二进制矩阵中的最短路径 | Shortest Path in Binary Matrix
    [Swift]LeetCode1092. 最短公共超序列 | Shortest Common Supersequence
  • 原文地址:https://www.cnblogs.com/luyingfeng/p/3548969.html
Copyright © 2020-2023  润新知