Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <map>
- using namespace std;
- #define ll long long
- #define dd double
- #define vv vector
- #define pb push_back
- #define forn( i, n ) for( ll i = 0; i < (ll) (n); i ++ )
- #define MOD 1000000021
- #define PR 37
- #define endl '\n'
- string num;
- ll numm[111];
- ll st[111];
- string s[51111];
- ll ss[511111][111];
- char c[26] = {
- '2', '2', '2', '3', '3', '3', '4', '4', '1', '1', '5', '5', '6', '6', '0', '7', '0', '7', '7', '8', '8', '8', '9', '9', '9', '0'
- // a b c d e f g h i j k l m n o p q r s t u v w x y z
- };
- vector< ll > v[111];
- vector< ll > ans;
- ll M;
- void f( string x, ll * a ) {
- a[0] = x[0] - '0';
- forn( i, x.size() ) {
- if( i == 0 )
- continue;
- a[i] = ( a[i - 1] * PR + x[i] - '0' ) % MOD;
- }
- }
- bool rec( ll i, ll j, ll m, bool r ) {
- if( m > M ) {
- return false;
- }
- i += s[j].size();
- if( i == num.size() ) {
- M = min( M, m );
- return r && M == m;
- }
- forn( k, v[i].size() ) {
- if( rec( i, v[i][k], m + 1, r ) ) {
- ans.pb( v[i][k] );
- return true;
- }
- }
- return false;
- }
- int main() {
- //cerr << (int) 'Z' << " " << (int) 'a' << endl;
- forn( i, 111 ) {
- if( i == 0 )
- st[i] = PR;
- else
- st[i] = ( st[i - 1] * PR ) % MOD;
- }
- cin >> num;
- while( num != "-1" ) {
- f( num, numm );
- //forn( i, num.size() ) {
- //cerr << numm[i] << endl;
- //}
- ll n;
- cin >> n;
- forn( i, num.size() )
- v[i].clear();
- ans.clear();
- M = num.size() + 1;
- forn( i, n ) {
- cin >> s[i];
- string w = "";
- forn( j, s[i].size() ) {
- if( s[i][j] >= 'a' )
- w += c[s[i][j] - 'a'];
- else
- w += c[s[i][j] - 'A'];
- }
- f( w, ss[i] );
- //cerr << w << endl;
- ll b = ss[i][s[i].size() - 1];
- forn( j, num.size() - s[i].size() + 1 ) {
- ll a = numm[j + s[i].size() - 1];
- if( j != 0 )
- a = ( a - numm[j - 1] * st[s[i].size() - 1] % MOD + MOD ) % MOD;
- //cerr << b << " " << a << endl;
- if( a == b )
- v[j].pb( i );
- }
- }
- forn( i, v[0].size() ) {
- rec( 0, v[0][i], 1, false );
- }
- if( M <= num.size() ) {
- forn( i, v[0].size() ) {
- if( rec( 0, v[0][i], 1, true ) ) {
- ans.pb( v[0][i] );
- break;
- }
- }
- forn( i, ans.size() )
- cout << s[ans[ans.size() - 1 - i]] << " ";
- } else {
- cout << "No solution.";
- }
- cout << endl;
- cin >> num;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement