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

第一次用STL做的,TLE了,自己构造字符串哈希函数才可以。。

TLE代码:

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int cnt = ,j;
char ss[];
string str,s,s1,s2;
map<string,int>v;
cin>>str;
s = str;
int len = str.size(); for (int i = ; i < len; i++)
{
s = str;
string::iterator it = s.begin();
for (j = ; j < i; j++)
ss[j] = str[j];
ss[j] = '\0';
s1 = ss;
s.erase(it,it+i);
s2 = s;
for (int k = ; k <= ; k++)
{
if (v[s1+s2]==)
{
v[s1+s2]++;
cnt++;
}
if (v[s2+s1]==)
{
v[s2+s1]++;
cnt++;
}
if (k==)
reverse(s1.begin(),s1.end());
else if (k==)
reverse(s2.begin(),s2.end());
else if (k==)
reverse(s1.begin(),s1.end());
}
}
printf("%d\n",cnt);
}
return ;
}

AC代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N=;
const int MOD=;
int cnt = ; struct node
{
char s[];
struct node *next;
}*hash[N]; void Hash(char *s1,char *s2)
{
char ss[],*str;
strcpy(ss,s1);
strcat(ss,s2);
str = ss;
unsigned int key = ;
while(*str)
{
key = key*+(*str++);
}
key%=;
if (!hash[key])
{
hash[key] = new node;
strcpy(hash[key]->s,ss);
hash[key]->next = NULL;
cnt++;
}
else
{
struct node *p = hash[key];
if(strcmp(p->s,ss)==)
return ;
while(p->next)
{
if (!strcmp(p->next->s,ss))
return ;
p = p->next;
}
p->next = new node;
strcpy(p->next->s,ss);
p->next->next = NULL;
cnt++;
}
}
int main()
{ char s[],s1[],s2[],s3[],s4[];
int n,j;
scanf("%d",&n);
while(n--)
{
cnt = ;
scanf("%s",s);
int len = strlen(s);
memset(hash,,sizeof(hash));
for (int i = ; i < len; i++)
{
for (j = ; j < i; j++)
s1[j] = s[j];
s1[j] = '\0';
for (j = i; j < len; j++)
s2[j-i] = s[j];
s2[j-i] = '\0';
strcpy(s3,s1);
strcpy(s4,s2);
reverse(s1,s1+i);
reverse(s2,s2+len-i);
Hash(s3,s4);
Hash(s4,s3);
Hash(s1,s4);
Hash(s4,s1);
Hash(s2,s3);
Hash(s3,s2);
Hash(s1,s2);
Hash(s2,s1); }
printf("%d\n",cnt);
}
return ;
}

有关字符串哈希函数的经典算法

 unsigned int SDBMHash(char *str)
{
unsigned int hash = ; while (*str)
{
// equivalent to: hash = 65599*hash + (*str++);
hash = (*str++) + (hash << ) + (hash << ) - hash;
} return (hash & 0x7FFFFFFF);
} // RS Hash Function
unsigned int RSHash(char *str)
{
unsigned int b = ;
unsigned int a = ;
unsigned int hash = ; while (*str)
{
hash = hash * a + (*str++);
a *= b;
} return (hash & 0x7FFFFFFF);
} // JS Hash Function
unsigned int JSHash(char *str)
{
unsigned int hash = ; while (*str)
{
hash ^= ((hash << ) + (*str++) + (hash >> ));
} return (hash & 0x7FFFFFFF);
} // P. J. Weinberger Hash Function
unsigned int PJWHash(char *str)
{
unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * );
unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt * ) / );
unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / );
unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
unsigned int hash = ;
unsigned int test = ; while (*str)
{
hash = (hash << OneEighth) + (*str++);
if ((test = hash & HighBits) != )
{
hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
} return (hash & 0x7FFFFFFF);
} // ELF Hash Function
unsigned int ELFHash(char *str)
{
unsigned int hash = ;
unsigned int x = ; while (*str)
{
hash = (hash << ) + (*str++);
if ((x = hash & 0xF0000000L) != )
{
hash ^= (x >> );
hash &= ~x;
}
} return (hash & 0x7FFFFFFF);
} // BKDR Hash Function
unsigned int BKDRHash(char *str)
{
unsigned int seed = ; // 31 131 1313 13131 131313 etc..
unsigned int hash = ; while (*str)
{
hash = hash * seed + (*str++);
} return (hash & 0x7FFFFFFF);
} // DJB Hash Function
unsigned int DJBHash(char *str)
{
unsigned int hash = ; while (*str)
{
hash += (hash << ) + (*str++);
} return (hash & 0x7FFFFFFF);
} // AP Hash Function
unsigned int APHash(char *str)
{
unsigned int hash = ;
int i; for (i=; *str; i++)
{
if ((i & ) == )
{
hash ^= ((hash << ) ^ (*str++) ^ (hash >> ));
}
else
{
hash ^= (~((hash << ) ^ (*str++) ^ (hash >> )));
}
} return (hash & 0x7FFFFFFF);
}

Organize Your Train part II(hash)的更多相关文章

  1. POJ 3007:Organize Your Train part II

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7561   Acce ...

  2. POJ 3007 Organize Your Train part II

    题意: 如上图所示,将一个字符串进行分割,反转等操作后不同字符串的个数: 例如字符串abba:可以按三种比例分割:1:3:2:2:3:1 部分反转可以得到如下所有的字符串: 去掉重复可以得到六个不同的 ...

  3. POJ 3007 Organize Your Train part II (字典树 静态)

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6478   Acce ...

  4. Organize Your Train part II 字典树(此题专卡STL)

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Acce ...

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

    http://poj.org/problem?id=3007 题意 :给你一个字符串,让你无论从什么地方分割,把这个字符串分成两部分s1和s2,然后再求出s3和s4,让你进行组合,看能出来多少种不同的 ...

  6. poj 3007 Organize Your Train part II(静态字典树哈希)

    Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6700 Accepted: 1922 Description RJ Freigh ...

  7. poj Organize Your Train part II

    http://poj.org/problem?id=3007 #include<cstdio> #include<algorithm> #include<cstring& ...

  8. poj 3007 Organize Your Train part II(二叉排序树)

    题目:http://poj.org/problem?id=3007 题意:按照图示的改变字符串,问有多少种..字符串.. 思路:分几种排序的方法,,刚开始用map 超时(map效率不高啊..),后来搜 ...

  9. Organize Your Train part II-POJ3007模拟

    Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Description RJ Freight, a Japane ...

随机推荐

  1. 第一个真正意义的jsp程序,连接msql数据库

    1. mysql数据库 :在百度上下一个安装包mysql-5.7.17.msi,只装服务即可,选择开发使用 2.建库,建表 mysql> create table  testdb; 导入相关建表 ...

  2. Mongo优化笔记

    最近MongoDb服务器负载比较高,容易出问题,这里把优化的方式整理一下. 1.由于各个项目组共用一个mongo实例,所以一个项目组的问题会影响到别的项目组,所以需要把各个项目的数据从一个实例中剥离出 ...

  3. 类QQ账号生成阐述

    具体需求如下: 数字账号从60000到9999999999(类似qq号一样的东东) 用户获取数字账号为随机分配,也可递加分配,需要符合如下规则 特殊账号需要保留,不能分配给用户,比如:112233(连 ...

  4. jenkins执行python脚本

    参考: https://blog.csdn.net/qq_39247153/article/details/81003244 https://blog.csdn.net/huashao0602/art ...

  5. Centos7安装gitlab服务器

    1.先按照官方教程 https://about.gitlab.com/downloads/#centos7 大概内容如下: 1. Install and configure the necessary ...

  6. 一文读懂架构师都不知道的isinstance检查机制

    起步 通过内建方法 isinstance(object, classinfo) 可以判断一个对象是否是某个类的实例.但你是否想过关于鸭子协议的对象是如何进行判断的呢? 比如 list 类的父类是继 o ...

  7. broker监控dataguard配置

    使用broker查看dataguard信息时有告警 DGMGRL> show configuration; Configuration - DRTEST Protection Mode: Max ...

  8. python编码小记

    Python编码小记 标签(空格分隔): 编程 python 1.list类型remove()操作 列表对象执行remove()函数后,会自动退出循环,所以如果想利用一个单独的for循环删除列表中多个 ...

  9. 64位CentOS6.5下Eclipse用Java连接mysql

    1.到官网上下载jdbc驱动,我下载的是mysql-connector-java-5.0.8.tar.gz 2.解压下载到的文件 tar -zxvf mysql-connector-java-5.0. ...

  10. 华为/H3C Syslog配置

    H3C交换机的设置举例1. 组网需求将系统的日志信息发送到 linux 日志主机:日志主机的IP 地址为1.2.0.1/16:信息级别高于等于 informational 的日志信息将会发送到日志主机 ...