Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <bitset>
- #include <deque>
- #include <cmath>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <iostream>
- #include <list>
- #include <map>
- #include <queue>
- #include <set>
- #include <sstream>
- #include <stack>
- #include <string>
- #include <utility>
- #include <vector>
- #include <functional>
- #include <numeric>
- #include <iomanip>
- #include <ctime>
- #define fst first
- #define snd second
- #define all(x) (x).begin(), (x).end()
- #define clr(a, v) memset(a, v, sizeof(a))
- #define pb push_back
- #define mp make_pair
- #define sz(x) (int)(x.size())
- #define FORN(i,s,n) for(int i=s;i<(int)(n);i++)
- #define FOR(i,n) FORN(i,0,n)
- #define FORIT(i,x) for( typeof x.begin() i=x.begin(); i!=x.end(); i++ )
- #define trace(x) cerr << #x << ": " << x << endl;
- #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
- using namespace std;
- typedef long long int64;
- typedef vector <int> vi;
- typedef pair <int,int> ii;
- typedef vector <string> vs;
- typedef vector <ii> vii;
- const int INF = 1e9+9;
- int DP[160][160][160];
- string s, key;
- vs word;
- int fDP(int pos_key, int pos_word, int c){
- if( key[pos_key]!=word[pos_word][c] ) return 0;
- if( DP[pos_key][pos_word][c]!=-1 ) return DP[pos_key][pos_word][c];
- int ans=0;
- if( pos_key==sz(key)-1 ){//estoy en la ultima letra
- if(pos_word==sz(word)-1) return DP[pos_key][pos_word][c]=1;//solo vale si es la ultima palabra
- else return DP[pos_key][pos_word][c]=0;
- }
- FORN( i,c+1,sz(word[pos_word ]) ) ans+=fDP(pos_key+1,pos_word ,i);//busco la siguiente lentra en la misma palabra
- if (pos_word!=sz(word)-1)
- FORN ( i,0 ,sz(word[pos_word+1]) ) ans+=fDP(pos_key+1,pos_word+1,i);// si no estoy en la ultima palabra busco en la siguiente
- return DP[pos_key][pos_word][c]=ans;
- }
- int main(){
- int N;
- while( cin>>N and N ){
- set <string> S; string proh;
- FOR(i,N) { cin>>proh; S.insert(proh); }
- char curr[160] ;
- gets(curr);
- while( gets(curr) ){
- proh=string(curr);
- if( proh.find("LAST CASE")==0 ) break;
- word.clear();
- istringstream S1(proh);
- S1>>key;
- string code=key;
- FOR(i,sz(key)) key[i]=tolower(key[i]);
- while( S1>>s ) if( !S.count(s) ) word.pb(s);
- clr(DP,-1);
- int ans=0;
- FOR(i,sz(word[0])) ans+=fDP( 0,0,i );
- if(ans) cout<<code<<" can be formed in "<<ans<<" ways"<<endl;
- else cout<<code<<" is not a valid abbreviation"<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement