看题目是字符串处理,但实际上是欧拉回路
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
const int LEN = 30;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct HEAD
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int in;
int out;
int next;
int last;
}head[LEN];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct NODE
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int num;
int weight;
int used;
int next;
int other;
}node[LEN*500];
int next_n;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void init_m ( int n )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
next_n = 0;
for ( int i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
head[i].in = head[i].out = 0;
head[i].next = -1;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void ins ( int a, int b, int weight )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
node[next_n].next = -1;
node[next_n].num = b;
node[next_n].used = 0;
node[next_n].weight = weight;
next_n ++;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if ( head[a].next==-1 )
head[a].next = next_n-1;
else
node[ head[a].last ].next = next_n-1;
head[a].last = next_n-1;
head[a].out ++;
head[b].in ++;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//预处理部分
struct SEGMENT
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
char str[LEN];
int len;
}seg[LEN*500];
int hash[LEN];
char ch[LEN];
int next_ch;
int flag[LEN];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void init ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
next_ch = 0;
for ( int i=0; i<LEN; i++ )
hash[i] = 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cmp1 ( const void *a, const void *b )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return strcmp ( ((SEGMENT *)a)->str, ((SEGMENT *)b)->str );
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cmp2 ( const void *a, const void *b )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return *(char *)a-*(char *)b;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void ser ( int used[], int s, int n, int &f )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
used[s] = 1;
f ++;
for ( int i=head[s].next; i!=-1; i=node[i].next )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( !used[ node[i].num ] )
ser ( used, node[i].num, n, f );
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int canget ( int used[], int p, int e )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
used[p] = 1;
if ( p==e )
return 1;
for ( int i=head[p].next; i!=-1; i=node[i].next )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( !node[i].used&&!used[ node[i].num ] )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( canget ( used, node[i].num, e ) )
return 1;
}
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void print ( int p, int total, int &count )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int flag = 0;
for ( int i=head[p].next; i!=-1; i=node[i].next )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( !node[i].used )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
flag = 1;
node[i].used = 1;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int used[LEN] = {0};
if ( canget ( used, node[i].num, p ) )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
printf ( "%s", seg[ node[i].weight ].str );
count ++;
if ( count!=total )
printf ( "." );
print ( node[i].num, total, count );
}
else
node[i].used = 0;
}
}
if ( flag )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( int i=head[p].next; i!=-1; i=node[i].next )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( !node[i].used )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
node[i].used = 1;
printf ( "%s", seg[ node[i].weight ].str );
count ++;
if ( count!=total )
printf ( "." );
print ( node[i].num, total, count );
}
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void work ( int n )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
qsort ( ch, next_ch, sizeof ( char ), cmp2 );
for ( int i=0; i<next_ch; i++ )
flag[ ch[i]-'a' ] = i;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
qsort ( seg, n, sizeof ( SEGMENT ), cmp1 );
init_m ( next_ch );
for ( int i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int a = flag[ seg[i].str[0]-'a' ];
int b = flag[ seg[i].str[ seg[i].len-1 ]-'a' ];
ins ( a, b, i );
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//检查连通性
int f = 0;
for ( int i=0; i<next_ch; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int used[LEN] = {0};
f = 0;
ser ( used, i, n, f );
if ( f==next_ch )
break;
}
if ( f<next_ch )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
printf ( "***\n" );
return;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//检查出入度
int ans1 = 0;
int ans2 = 0;
int no1, no2;
for ( int i=0; i<next_ch; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( head[i].in-head[i].out==1 )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ans1 ++;
no1 = i;
}
else if ( head[i].in-head[i].out==-1 )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ans2 ++;
no2 = i;
}
else if ( abs (head[i].in-head[i].out)>1 )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ans1 = ans2 = LEN;
}
}
if ( !((ans1==0&&ans2==0)||(ans1==1&&ans2==1)) )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
printf ( "***\n" );
return;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//查找路径
int count = 0;
if ( ans1==0&&ans2==0 )
print ( 0, n, count );
else
print ( no2, n, count );
printf ( "\n" );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int t;
scanf ( "%d", &t );
while ( t-- )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int n;
scanf ( "%d", &n );
init ();
for ( int i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf ( "%s", seg[i].str );
seg[i].len = strlen ( seg[i].str );
if ( !hash[ seg[i].str[0]-'a' ] )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ch[next_ch++] = seg[i].str[0];
hash[ seg[i].str[0]-'a' ] = 1;
}
if ( !hash[ seg[i].str[seg[i].len-1]-'a' ] )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ch[next_ch++] = seg[i].str[seg[i].len-1];
hash[ seg[i].str[seg[i].len-1]-'a' ] = 1;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
work ( n );
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论
![](/images/xml.gif)
阅读排行榜
评论排行榜
|
|