POJ 3007-Organize Your Train part II(hash-字符串)
来源:互联网 发布:淘宝宝贝详情文字描述 编辑:程序博客网 时间:2024/04/30 11:10
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
4aaabbaabcdabcde
Sample Output
161218
Source
题目意思
解题思路
#include<iostream>#include<cstdio>#include<iomanip>#include<cmath>#include<cstdlib>#include<cstring>#include<map>#include<algorithm>#include<vector>#include<queue>using namespace std;#define INF 0xfffffffconst int MAXN=99991;const int MAXM=100;char c[MAXM];//原始串char s1[MAXM],s1[MAXM];//分割的两个串char t1[MAXM],t2[MAXM];//保留原始分割的两个串char s[MAXM];//最终连结的串//map<std::string,bool> ma;void reserve(char x[])//反转字符串{ char temp[MAXN]; int n=strlen(x),cnt=0; for(int i=n-1; i>=0; --i) temp[cnt++]=x[i]; temp[cnt]='\0'; strcpy(x,temp);}typedef struct node{ char str[MAXM]; node *next; node() { next=0; }} Point;Point *Head[MAXN];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%=MAXN; } 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;}int main(){#ifdef ONLINE_JUDGE#else freopen("G:/cbx/read.txt","r",stdin); //freopen("G:/cbx/out.txt","w",stdout);#endif //ios::sync_with_stdio(false); //cin.tie(0); int t; scanf("%d",&t); while(t--) { scanf("%s",&c); int len=strlen(c); int ans=0; for(int k=1; k<len; ++k)//分割点 { memset(s1,'\0',sizeof(s1)); int cnt1=0;//第一串长度 for(int i=0; i<k; ++i)//第一串 s1[cnt1++]=c[i]; cnt1=strlen(s1); memset(s2,'\0',sizeof(s2)); int cnt2=0;//第二串长度 for(int i=k; i<len; ++i)//第二串 s2[cnt2++]=c[i]; memcpy(t1,s1,cnt1); memcpy(t2,s2,cnt2); //上上 reserve(s1); reserve(s2); int cnt=0; for(int i=0; i<cnt1; ++i) s[cnt++]=s1[i]; for(int i=0; i<cnt2; ++i) s[cnt++]=s2[i]; //cout<<s<<" "<<s1<<" "<<s2<<endl; if(Hash(s)) ++ans;//记录 cnt=0; for(int i=0; i<cnt2; ++i) s[cnt++]=s2[i]; for(int i=0; i<cnt1; ++i) s[cnt++]=s1[i]; //cout<<s<<" "<<s1<<" "<<s2<<endl; if(Hash(s)) ++ans;//记录 //下下 memcpy(s1,t1,cnt1); memcpy(s2,t2,cnt2); cnt=0; for(int i=0; i<cnt1; ++i) s[cnt++]=s1[i]; for(int i=0; i<cnt2; ++i) s[cnt++]=s2[i]; //cout<<s<<" "<<s1<<" "<<s2<<endl; if(Hash(s)) ++ans;//记录 cnt=0; for(int i=0; i<cnt2; ++i) s[cnt++]=s2[i]; for(int i=0; i<cnt1; ++i) s[cnt++]=s1[i]; //cout<<s<<" "<<s1<<" "<<s2<<endl; if(Hash(s)) ++ans;//记录 //上下 memcpy(s1,t1,cnt1); memcpy(s2,t2,cnt2); reserve(s1); cnt=0; for(int i=0; i<cnt2; ++i) s[cnt++]=s2[i]; for(int i=0; i<cnt1; ++i) s[cnt++]=s1[i]; //cout<<s<<" "<<s1<<" "<<s2<<endl; if(Hash(s)) ++ans;//记录 cnt=0; for(int i=0; i<cnt1; ++i) s[cnt++]=s1[i]; for(int i=0; i<cnt2; ++i) s[cnt++]=s2[i]; //cout<<s<<" "<<s1<<" "<<s2<<endl; if(Hash(s)) ++ans;//记录 //下上 memcpy(s1,t1,cnt1); memcpy(s2,t2,cnt2); reserve(s2); cnt=0; for(int i=0; i<cnt1; ++i) s[cnt++]=s1[i]; for(int i=0; i<cnt2; ++i) s[cnt++]=s2[i]; //cout<<s<<" "<<s1<<" "<<s2<<endl; if(Hash(s)) ++ans;//记录 cnt=0; for(int i=0; i<cnt2; ++i) s[cnt++]=s2[i]; for(int i=0; i<cnt1; ++i) s[cnt++]=s1[i]; //cout<<s<<" "<<s1<<" "<<s2<<endl; if(Hash(s)) ++ans;//记录 } printf("%d\n",ans); } return 0;}
- POJ-3007 Organize Your Train part II (字符串hash)
- POJ 3007-Organize Your Train part II(hash-字符串)
- 【POJ 3007】 Organize Your Train part II (字符串HASH)
- [POJ 3007]Organize Your Train part II[Hash][Trie]
- POJ 3007 Organize Your Train part II
- POJ 3007 Organize Your Train part II
- POJ 3007 Organize Your Train part II
- POJ 3007:Organize Your Train part II
- poj-3007 Organize Your Train part II
- poj 3007 Organize Your Train part II
- POJ 3007 - Organize Your Train part II
- poj 3007Organize Your Train part II
- POJ Organize Your Train part II 3007
- poj-3007-Organize Your Train part II
- poj3007 Organize Your Train part II, 字符串hash
- poj 3007 Organize Your Train part II (字符串哈希)
- poj 3007 Organize Your Train part II(字符串哈希)
- poj3007--Organize Your Train part II(hash)
- FTPS 与SFTP大对决
- JAVA泛型通配符<? extends E>
- linux下部署go
- B1044.火星数字
- 添加Fragment报已有父view,需先移除的错
- POJ 3007-Organize Your Train part II(hash-字符串)
- Android Studio关于SVN的相关配置及从SVN检出项目
- 用parcel解决intent传递大数据
- java线程安全问题之静态成员变量、实例成员变量、局部变量
- Java中的CopyOnWriteArraySet使用和实现原理
- break continue 在 for循环和 switch 的 不同
- adb常用命令
- 机器学习的大局观:使用神经网络和TensorFlow来对文本分类
- 关于tinker自动生成application失败的问题