hdu1181變形課
典型的Floyd傳遞閉包,只要知道了算法,做起來和簡單。
#include <stdio.h>
#include <string.h>
#include <memory.h>
#define DEBUG 1
const int N=28 ;
int map[N][N] ;

void Floyd( )


{
int i, j, k ;
for( k=0; k<26; ++k )
for( i=0; i<26; ++i )
for( j=0; j<26; ++j )
map[i][j] = map[i][j] || ( map[i][k] && map[k][j] ) ;
}

int main()


{
#if DEBUG
freopen("C:\\Documents and Settings\\Administrator\\桌面\\in.txt","r",stdin);
freopen("C:\\Documents and Settings\\Administrator\\桌面\\out.txt","w",stdout);
#endif
int len ;
char a[2000] ;

while( EOF != scanf("%s",a) )
{

if( a[0] == '0' )
{
Floyd( ) ;
if( map[1][12] )
printf("Yes.\n") ;
else
printf("No.\n") ;
memset( map, 0, sizeof(map) ) ;
continue ;
}
len = strlen( a ) ;
map[a[0]-'a'][a[len-1]-'a'] = 1 ;
}
return 0 ;
}
#include <stdio.h>
#include <string.h>
#include <memory.h>
#define DEBUG 1
const int N=28 ;
int map[N][N] ;
void Floyd( )

{
int i, j, k ;
for( k=0; k<26; ++k )
for( i=0; i<26; ++i )
for( j=0; j<26; ++j )
map[i][j] = map[i][j] || ( map[i][k] && map[k][j] ) ;
}
int main()

{
#if DEBUG
freopen("C:\\Documents and Settings\\Administrator\\桌面\\in.txt","r",stdin);
freopen("C:\\Documents and Settings\\Administrator\\桌面\\out.txt","w",stdout);
#endif
int len ;
char a[2000] ;
while( EOF != scanf("%s",a) )
{
if( a[0] == '0' )
{
Floyd( ) ;
if( map[1][12] )
printf("Yes.\n") ;
else
printf("No.\n") ;
memset( map, 0, sizeof(map) ) ;
continue ;
}
len = strlen( a ) ;
map[a[0]-'a'][a[len-1]-'a'] = 1 ;
}
return 0 ;
}

