Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define sc( x ) scanf( "%d" , &x )
- #define REP( i , n ) for( int i = 0 ; i < n ; ++i )
- #define clr( t , val ) memset( t , val , sizeof( t ) )
- #define pb push_back
- #define all( v ) v.begin() , v.end()
- #define SZ( v ) ((int)(v).size())
- #define fi first
- #define se second
- #define mp make_pair
- #define N 1000000
- #define B 4LL
- #define MOD1 1000000007LL
- #define MOD2 1000000009LL
- typedef long long ll;
- typedef vector< int > vi;
- typedef pair< int , int > pii;
- typedef pair< ll , ll > pll;
- typedef vector< pii > vpii;
- typedef vector< pll > vpll;
- void format( string &s ){
- REP( i , SZ( s ) ) s[ i ] -= 'a' , s[ i ] ++ ;
- }
- pll getHash( string s ){
- ll H1 = 0 , H2 = 0;
- REP( i , SZ( s ) ){
- H1 = ( H1 * B + s[ i ] )%MOD1;
- H2 = ( H2 * B + s[ i ] )%MOD2;
- }
- return mp( H1 , H2 );
- }
- ll POT1[ N + 5 ] , POT2[ N + 5 ];
- ll table1[ N + 5 ] , table2[ N + 5 ];
- ll fix( ll x , ll mod ){
- return (x %mod + mod) %mod;
- }
- ll f1( int lo , int hi ){
- if( lo > hi ) return 0;
- return fix(table1[ hi + 1 ] - table1[ lo ] * POT1[ hi - lo +1 ] , MOD1 );
- }
- ll f2( int lo , int hi ){
- if( lo > hi ) return 0;
- return fix(table2[ hi + 1 ] - table2[ lo ] * POT2[ hi - lo +1 ] , MOD2 );
- }
- int main(){
- POT1[ 0 ] = POT2[ 0 ] = 1;
- for( int i = 1 ; i <= N ; ++i ){
- POT1[ i ] = ( POT1[ i - 1 ] * B )%MOD1;
- POT2[ i ] = ( POT2[ i - 1 ] * B )%MOD2;
- }
- ios_base :: sync_with_stdio( 0 );
- int n , m;
- while( cin >> n >> m ){
- vpll vec;
- REP( i , n ){
- string s;
- cin >> s;
- format( s );
- vec.pb( getHash( s ) );
- }
- sort( all( vec ) );
- REP( it , m ){
- string s;
- cin >> s;
- format( s );
- pll x = getHash( s );
- REP( i , SZ( s ) ){
- table1[ i + 1 ] = ( table1[ i ] * B + s[ i ] )%MOD1;
- table2[ i + 1 ] = ( table2[ i ] * B + s[ i ] )%MOD2;
- }
- bool found = 0;
- REP( i , SZ( s ) ){
- for( ll j = 1 ; j <= 3 ; ++j ){
- if( j == s[ i ] ) continue;
- int len = SZ( s ) - 1 - (i + 1) + 1;
- ll H1 = ( f1( 0 , i - 1 ) * POT1[ len + 1 ] + f1( i + 1 , SZ( s ) - 1 ) + POT1[ len ] * j )%MOD1;
- ll H2 = ( f2( 0 , i - 1 ) * POT2[ len + 1 ] + f2( i + 1 , SZ( s ) - 1 ) + POT2[ len ] * j )%MOD2;
- x = mp( H1 , H2 );
- if( binary_search( all( vec ) , x ) ){
- found = 1;
- break;
- }
- }
- }
- if( found ) cout << "YES\n";
- else cout << "NO\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment