POJ 3007-Organize Your Train part II(hash-字串)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8523 | Accepted: 2435 |
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
4 aa abba abcd abcde
Sample Output
1 6 12 18
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 0xfffffff
const 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;
}
相關文章
- 「譯」MotionLayout 介紹 (part II)
- Business Objects Query Builder – Part IIObjectUI
- Delphi物件模型(Part II) (轉)物件模型
- Writing your first Django app, part 1DjangoAPP
- POJ 3981 字串替換字串
- 字串的調整II字串
- 【做題記錄】ds合集 Part II
- Structure of Linux Kernel Device Driver(Part II)StructLinuxdev
- 前端測試:Part II (單元測試)前端
- oracle administrator's guide Part IIOracleGUIIDE
- On Designing Good Libraries -- Part II (轉)Go
- Leedcode-反轉字串 II字串
- trainAI
- Part II 診斷和優化資料庫效能優化資料庫
- React + Koa 實現服務端渲染(SSR) Part IIReact服務端
- oracle database backup and recovery user's guide part I & IIOracleDatabaseGUIIDE
- POJ 1261 Period KMP (字串週期)KMP字串
- POJ 1159 Palindrome(字串變回文:LCS)字串
- 第四章 字串part01字串
- Part II 建立和配置CDB-Oracle多租戶管理員指南Oracle
- Python3.4 Tutorial11 - Brief Tour of the Standard Library – Part IIPython
- Lesson12——NumPy 字串函式之 Part1:字串操作函式字串函式
- Day7(字串)|344.反轉字串 541.反轉字串II 54.替換數字字串
- Your Prediction Gets As Good As Your DataGo
- transformers、torch train demoORMAI
- 幾乎所有基於Java的web app都需要Model 2 (part II) (轉)JavaWebAPP
- 3298. 統計重新排列後包含另一個字串的子字串數目 II字串
- POJ--2406Power Strings+KMP求字串最小週期KMP字串
- Your title
- Codeforces 8A. Train and PeterAI
- tf.train.NewCheckpointReaderAI
- Part II 配置和管理多租戶環境概述-Oracle多租戶管理員指南Oracle
- [20130812]12c Partial Indexes For Partitioned Tables Part II.txtIndex
- [20130815]12c Asynchronous Global Index Maintenance Part II.txtIndexAINaN
- POJ 3415 Common Substrings(字尾陣列求重複字串)陣列字串
- 08天【程式碼隨想錄演算法訓練營34期】第四章 字串part01(● 344.反轉字串 ● 541. 反轉字串II ● 卡碼網:54.替換數字 ● 151.翻轉字串裡的單詞 ● 卡碼網:55.右旋轉字串)演算法字串
- 車廂排程(train.cpp)AI
- Prettier your projectProject