Question:
Given a string, cut it from any position into two halves to get two substrings
Define substring 1 as S1, substring 2 as S2, substring 1 as S3, and substring 2 as S4
The following code extracts two string combinations from S1 S2 S3 S4 and asks how many different combinations are available:
#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <set>#include <map>#include <string>#include <stack>#include <queue>#include <vector>#define for0(a,b) for(a=0;a<b;++a)#define for1(a,b) for(a=1;a<=b;++a)#define foru(i,a,b) for(i=a;i<=b;++i)#define ford(i,a,b) for(i=a;i>=b;--i)using namespace std;typedef double db;typedef long long ll;const db eps = 1e-8;const int inf = 1e9;const int M = 1e4;const int maxn = 80;char s[maxn], p[maxn], q[maxn], rp[maxn], rq[maxn], str[maxn];char val[M][maxn];int cnt, n;//BKDR Hash Functionint hash(char *v){ unsigned int seed = 131; unsigned int hash = 0; for(int i=0; v[i]; ++i) hash = hash * seed + v[i]; return (hash & 0x7FFFFFFF) % M;}void contact(char *a, char *b){ int k = 0; for(int i=0; a[i]; ++i) str[k++] = a[i]; for(int i=0; b[i]; ++i) str[k++] = b[i]; str[k] = '\0';}void insert(char *a, char *b){ contact(a, b); int k = hash(str); while( (strlen(val[k])>0) && strcmp(str,val[k])!=0){ k = (k+1) % M; } if(strlen(val[k])== 0){ strcpy(val[k], str); cnt++; }}int main(){#ifndef ONLINE_JUDGE freopen("in.cpp", "r", stdin); freopen("out.cpp","w", stdout);#endif // ONLINE_JUDGE int n, i, j; scanf("%d", &n); while(n--) { scanf("%s", s); for0(i,M) val[i][0]='\0'; int len = strlen(s); int k; cnt = 0; for1(i,len-1) { k = 0; for0(j,i){ p[j] = s[j]; rp[k++] = s[i-j-1]; } p[k] = rp[k] = '\0'; k = 0; foru(j,i,len-1) { q[j-i] = s[j]; rq[k] = s[len-k-1]; ++k; } q[k] = rq[k] = '\0'; insert(p, q); insert(q, p); insert(p, rq); insert(rq, p); insert(q, rp); insert(rp, q); insert(rp, rq); insert(rq, rp); } printf("%d\n", cnt); } return 0;}
Poj3007 organize your train part II, string hash