poj 3007 Organize Your Train part II (字符串哈希)
2016-01-28 09:25
555 查看
这应该是一道考STL的题,然而用STL试过2^n遍后,只剩下伤心与落泪
没办法,还是得用字符串哈希
没办法,还是得用字符串哈希
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <list> #include <vector> #include <algorithm> #include <iomanip> #define RR freopen("in.txt","r"m,stdin) #define WW freopen("out.txt","w",stdout) #define LL long long //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1e6; const double eps = 1e-9; const int Max = 26; int ans; struct node { char str[80]; node *next; node():next(0){} }*Head[100010]; 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) { node *p = new node; strcpy(p->str,s); Head[num]=p; return true; } else { node *p = Head[num]; while(p) { if(strcmp(p->str,s)==0) return false; p=p->next; } p=new node; strcpy(p->str,s); p->next = Head[num]; Head[num] = p; } return true; } void Merge(char *s1, char *s2) { char s[101]; int top = 0; for(int i=0; s1[i]; i++) s[top++] = s1[i]; for(int i=0; s2[i]; i++) s[top++] = s2[i]; s[top] = '\0'; if(Hash(s)) ans++; } void Reverse(char *s) { int len = strlen(s); for(int i=0;i<len/2;i++) swap(s[i],s[len-i-1]); } void Add(char *s1, char *s2) { Merge(s1,s2); Merge(s2,s1); } void Split(int pos, int len ,char *str, char *s1, char *s2) { for(int i=0;i<pos;i++) s1[i] = str[i]; s1[pos] = '\0'; for(int i=pos,j=0; i<len; i++,j++) s2[j] = str[i]; s2[len-pos] = '\0'; Add(s1,s2); Reverse(s1); Add(s1,s2); Reverse(s2); Add(s1,s2); Reverse(s1); Add(s1,s2); } int main() { int T; scanf("%d",&T); char str[80],s1[80],s2[80]; while(T-- && scanf("%s", str)) { memset(Head, 0, sizeof(Head)); ans = 0; int len = strlen(str); for(int i=1; i<len; i++) Split(i,len,str,s1,s2); cout<<ans<<endl; } return 0; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- 【高手回避】poj3268,一道很水的dijkstra算法题