Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1.     #include <algorithm>
  2.     #include <bitset>
  3.     #include <deque>
  4.     #include <cmath>
  5.     #include <cstdio>
  6.     #include <cstdlib>
  7.     #include <cstring>
  8.     #include <iostream>
  9.     #include <list>
  10.     #include <map>
  11.     #include <queue>
  12.     #include <set>
  13.     #include <sstream>
  14.     #include <stack>
  15.     #include <string>
  16.     #include <utility>
  17.     #include <vector>
  18.     #include <functional>
  19.     #include <numeric>
  20.     #include <iomanip>
  21.     #include <ctime>
  22.  
  23.     #define fst first
  24.     #define snd second
  25.     #define all(x) (x).begin(), (x).end()
  26.     #define clr(a, v) memset(a, v, sizeof(a))
  27.     #define pb push_back
  28.     #define mp make_pair
  29.     #define sz(x) (int)(x.size())
  30.     #define FORN(i,s,n) for(int i=s;i<(int)(n);i++)
  31.     #define FOR(i,n) FORN(i,0,n)
  32.     #define FORIT(i,x) for( typeof x.begin()  i=x.begin(); i!=x.end(); i++ )
  33.     #define trace(x)    cerr << #x << ": " << x << endl;
  34.     #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
  35.  
  36.     using namespace std;
  37.  
  38.     typedef long long int64;
  39.     typedef vector <int> vi;
  40.     typedef pair <int,int> ii;
  41.     typedef vector <string> vs;
  42.     typedef vector <ii> vii;
  43.  
  44.     const int INF = 1e9+9;
  45.     int DP[160][160][160];
  46.     string s, key;
  47.     vs word;
  48.  
  49.     int fDP(int pos_key, int pos_word, int c){
  50.         if( key[pos_key]!=word[pos_word][c] ) return 0;
  51.         if( DP[pos_key][pos_word][c]!=-1 ) return DP[pos_key][pos_word][c];
  52.         int ans=0;
  53.         if( pos_key==sz(key)-1 ){//estoy en la ultima letra
  54.             if(pos_word==sz(word)-1) return DP[pos_key][pos_word][c]=1;//solo vale si es la ultima palabra
  55.             else                     return DP[pos_key][pos_word][c]=0;
  56.         }
  57.         FORN( i,c+1,sz(word[pos_word  ]) )      ans+=fDP(pos_key+1,pos_word  ,i);//busco la siguiente lentra en la misma palabra
  58.         if (pos_word!=sz(word)-1)
  59.             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
  60.         return DP[pos_key][pos_word][c]=ans;
  61.     }
  62.  
  63.     int main(){
  64.         int N;
  65.         while( cin>>N and N ){
  66.             set <string> S; string proh;
  67.             FOR(i,N) { cin>>proh; S.insert(proh); }
  68.             char curr[160] ;
  69.             gets(curr);
  70.             while( gets(curr) ){
  71.                 proh=string(curr);
  72.                 if( proh.find("LAST CASE")==0 ) break;
  73.                 word.clear();
  74.                 istringstream S1(proh);
  75.                 S1>>key;
  76.                 string code=key;
  77.                 FOR(i,sz(key)) key[i]=tolower(key[i]);
  78.                 while( S1>>s ) if( !S.count(s) ) word.pb(s);
  79.                 clr(DP,-1);
  80.                 int ans=0;
  81.                 FOR(i,sz(word[0])) ans+=fDP( 0,0,i );
  82.                 if(ans) cout<<code<<" can be formed in "<<ans<<" ways"<<endl;
  83.                 else cout<<code<<" is not a valid abbreviation"<<endl;
  84.             }
  85.         }
  86.         return 0;
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement