1. /*************FORHAD-SUST-BD*****************/
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <map>
  6. #include <queue>
  7. #include <stack>
  8. #include <algorithm>
  9. #include <list>
  10. #include <set>
  11. #include <cmath>
  12. #include <cstring>
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <sstream>
  17. #include <stdlib.h>
  18. #include <vector>
  19. #include <iomanip>
  20.  
  21. using namespace std;
  22.  
  23. #ifdef __GNUC__
  24. typedef long long ll;typedef unsigned long long ull;
  25. #else
  26. typedef __int64 ll; typedef unsigned __int64 ull;
  27. #endif
  28.  
  29. #define INF 1<<28
  30. #define SIZE 100
  31. #define ERR 1e-7
  32. #define PI 2*acos(0)
  33. #define REP(i,n) for (i=0;i<n;i++)
  34. #define REPE(i,n) for (i=0;i<=n;i++)
  35. #define REV(i,n) for (i=n;i>=0;i--)
  36. #define FOR(i,p,k) for (i=p; i<k;i++)
  37. #define FORE(i,a,b) for (int i=a; i<=b; i++)
  38. #define FOREACH(it,x) for(__typeof((x).begin()) it=(x.begin()); it!=(x).end(); ++it)
  39.  
  40. #define bug(x) cout<< "->" <<#x<<": "<<x<<endl
  41. #define Sort(x) sort(x.begin(),x.end())
  42. #define Reverse(x) reverse(x.begin(),x.end())
  43. #define MP(a,b) make_pair(a,b)
  44. #define Clear(x,with) memset(x,with,sizeof(x))
  45. #define Copy(c,r) memcpy(c,r,sizeof(r))
  46. #define SZ(x) (int)x.size()
  47. #define length(x) (int)x.length()
  48. #define All(x) x.begin(),x.end()
  49. #define pb push_back
  50. #define popcount(i) __builtin_popcount(i)
  51. #define gcd(a,b) __gcd(a,b)
  52. #define fs first
  53. #define sc second
  54. #define two(X) (1<<(X))
  55. #define twoL(X) (((ll)(1))<<(X))
  56. #define setBit(mask,i) (mask|two(i))
  57. #define contain(S,X) (((S)&two(X))!=0)
  58. #define containL(S,X) (((S)&twoL(X))!=0)
  59. #define maximum(v) *max_element(All(v))
  60. #define minimum(v) *min_element(All(v))
  61. #define CS c_str()
  62. #define findx(a,x) (find(All(a),x)-a.begin())
  63. #define Unique(v) sort((v).begin(),(v).end()),v.erase(unique(v.begin(),v.end()),v.end())
  64.  
  65. typedef pair<int,int> pii;
  66. typedef pair<double,double> pdd;
  67. typedef vector<int> vi;
  68. typedef vector<double>vd;
  69. typedef vector<ll>vll;
  70. typedef vector<string> vs;
  71. typedef vector<vi>vvi;
  72. typedef vector<vll>vvll;
  73. typedef vector<vd>vvd;
  74. typedef vector<pii>vpii;
  75. typedef map<string,int> msi;
  76. typedef map<int,int>mii;
  77. typedef map<pii,int>mpi;
  78.  
  79. template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}
  80. template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}
  81. template<class T> T Abs(T x) {return x > 0 ? x : -x;}
  82. template<class T> inline T sqr(T x){return x*x;}
  83. template<class T> inline bool isPrime(T n){if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}
  84. template<class T> inline T Mod(T n,T m) {return (n%m+m)%m;} //For Positive Negative No.
  85. //For debugging
  86. template<class T> void debug(const T& e){cout<<e<<endl;}
  87. template<class T1,class T2> void debug(const T1& e1,const T2& e2){cout<<e1<<"\t"<<e2<<endl;}
  88. template<class T1,class T2,class T3> void debug(const T1& e1,const T2& e2,const T3& e3){cout<<e1<<"\t"<<e2<<"\t"<<e3<<endl;}
  89. template<class T1,class T2,class T3,class T4> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<endl;}
  90. template<class T1,class T2,class T3,class T4,class T5> void debug(const T1& e1,const T2& e2,const T3& e3,const T4& e4,const T5& e5){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<"\t"<<e5<<endl;}
  91. template<class T> void debug(vector<T>& e){int i;REP(i,SZ(e)) cout<<e[i]<<" ";cout<<endl;}
  92. template<class T> void debug(vector< basic_string<T> >& e){int i,j;REP(i,SZ(e)) {REP(j,SZ(e[i])) cout<<e[i][j];cout<<endl;} cout<<endl;}
  93. template<class T> void debug(vector< vector<T> >& e,int row,int col){int i,j;REP(i,row) {REP(j,col) cout<<e[i][j]<<"\t";cout<<endl;} cout<<endl;}
  94. template<class T> void debug(T e[SIZE][SIZE],int row,int col){int i,j;REP(i,row) {REP(j,col) cout<<e[i][j]<<" ";cout<<endl;}}
  95. template<class T> void debug(T e[],int row){int i;REP(i,row) cout<<e[i]<<" ";cout<<endl;}
  96.  
  97. //Important Functions
  98.  
  99. //int,double is converted to string
  100. template<class T> string toString(T n){ostringstream oss;oss<<n;oss.flush();return oss.str();}
  101. //string is converted to int
  102. int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
  103. //string is converted to Long long
  104. ll toLl(string s){ll r=0;istringstream sin(s); sin>>r; return r;}
  105. //check character is vowel
  106. bool IsVowel(char ch){ch=tolower(ch);if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')return true;return false;}
  107. //compute b^p
  108. ll Pow(ll B,ll P){ ll R=1; while(P>0) {if(P%2==1) R=(R*B);P/=2;B=(B*B);}return R;}
  109. //compute b^p%m
  110. int BigMod(ll B,ll P,ll M){ ll R=1; while(P>0) {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return (int)R;}
  111. /*calculates (a*b)%c taking into account that a*b might overflow */
  112. ll mulmod(ll a,ll b,ll c){ ll x = 0,y=a%c; while(b>0){ if(b%2 == 1){x=(x+y)%c;} y=(y*2)%c;b/= 2;}return x%c;}
  113. //print a number in binary format with n length
  114. void binprint(ll mask,ll n){ll i;string s="";do{s+=(mask%2+'0');mask/=2;}while(mask);Reverse(s);s=string(max(n-SZ(s),0LL),'0')+s;for(i=SZ(s)-n;i<SZ(s);i++) printf("%c",s[i]);printf("\n");}
  115. //ASCII Chart
  116. void ASCII_Chart(){int i,j,k;printf("ASCII Chart:(30-129)\n");FOR(i,30,50){REP(j,5){k=i+j*20;printf("%3d---> '%c' ",k,k);}printf("\n");}}
  117. //SubstringGenerate
  118. vector<string> SubstringGenerate(string str){int i,j,len;vs store;len=SZ(str);REP(i,len) FOR(j,i,len)store.pb(str.substr(i,j-i+1));return store;}
  119.  
  120.  
  121. //struct pq{ int cost,node;bool operator<(const pq &b)const{return cost>b.cost;}};// Min Priority Queue
  122. //bool comp(pq a,pq b){ return a.cost < b.cost;} //Asc Sort by cost
  123.  
  124. //int month[]={31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
  125.  
  126. //int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
  127. //int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
  128. //int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
  129. //int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; //Hexagonal Direction
  130.  
  131. #include<conio.h> //for using getch();
  132.  
  133. vector<string>dict;
  134. int N;
  135.  
  136. int T9[]={
  137. 2,2,2,
  138. 3,3,3,
  139. 4,4,4,
  140. 5,5,5,
  141. 6,6,6,
  142. 7,7,7,7,
  143. 8,8,8,
  144. 9,9,9,9
  145. };
  146.  
  147. int count(string word)
  148. {
  149. string pattern,temp;
  150. int i,j,cost;
  151.  
  152. REP(i,SZ(word))
  153. {
  154. pattern+=T9[word[i]-'a']+'a';
  155. }
  156. cost=0;
  157. REP(i,N)
  158. {
  159. if(SZ(dict[i])<SZ(word)) continue;
  160. if(dict[i].substr(0,SZ(word))==word) return cost;
  161. temp="";
  162. REP(j,SZ(word))
  163. {
  164. temp+=T9[dict[i][j]-'a']+'a';
  165. }
  166. if(temp==pattern) cost++; //For Next in dictionary
  167. }
  168. return INF;
  169. }
  170.  
  171. int dp[SIZE],len;
  172. string msg;
  173.  
  174. int rec(int i)
  175. {
  176. int &ret=dp[i],j;
  177. string word;
  178.  
  179. if(ret!=-1) return ret;
  180.  
  181. if(i==len) return ret=-1;
  182.  
  183. ret=INF;
  184.  
  185. word="";
  186. FOR(j,i,len)
  187. {
  188. word+=msg[j];
  189. ret=min(ret,rec(j+1)+(j-i+1)+count(word)+1);
  190. }
  191. return ret;
  192. }
  193.  
  194. class JohnnysPhone
  195. {
  196. public:
  197. int minimizePushes(vector <string> dictionary, string message)
  198. {
  199. int i,n,res,space;
  200. string str;
  201.  
  202. n=SZ(dictionary);
  203. dict.clear();
  204. REP(i,n)
  205. {
  206. istringstream iss(dictionary[i]);
  207. while(iss>>str)
  208. {
  209. dict.pb(str);
  210. }
  211. }
  212. N=SZ(dict);
  213. istringstream iss(message);
  214. res=0;
  215. space=0;
  216. REP(i,SZ(message))
  217. if(message[i]==' ')
  218. space++;
  219. while(iss>>str)
  220. {
  221. msg=str;
  222. len=SZ(msg);
  223. Clear(dp,-1);
  224. res+=rec(0);
  225. if(res>=INF) return -1;
  226. }
  227. return res+space;
  228. }
  229. };
  230.  
  231.  
  232. template<typename T> void print( T a ) {
  233. cerr << a;
  234. }
  235.  
  236. void print( long long a ) {
  237. cerr << a << "L";
  238. }
  239.  
  240. void print( string a ) {
  241. cerr << '"' << a << '"';
  242. }
  243.  
  244. template<typename T> void print( vector<T> a ) {
  245. cerr << "{";
  246. for ( int i = 0 ; i != a.size() ; i++ ) {
  247. if ( i != 0 ) cerr << ", ";
  248. print( a[i] );
  249. }
  250. cerr << "}" << endl;
  251. }
  252.  
  253. template<typename T> void assert_eq( int n, T have, T need ) {
  254. if ( have == need ) {
  255. cerr << "Case " << n << " passed." << endl;
  256. } else {
  257. cerr << "Case " << n << " failed: expected ";
  258. print( need );
  259. cerr << " received ";
  260. print( have );
  261. cerr << "." << endl;
  262. }
  263. }
  264.  
  265. template<typename T> void assert_eq( int n, vector<T> have, vector<T> need ) {
  266. if( have.size() != need.size() ) {
  267. cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
  268. print( have );
  269. print( need );
  270. return;
  271. }
  272. for( int i= 0; i < have.size(); i++ ) {
  273. if( have[i] != need[i] ) {
  274. cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
  275. print( have );
  276. print( need );
  277. return;
  278. }
  279. }
  280. cerr << "Case " << n << " passed." << endl;
  281. }
  282. void assert_eq( int n, string have, string need ) {
  283. if ( have == need ) {
  284. cerr << "Case " << n << " passed." << endl;
  285. } else {
  286. cerr << "Case " << n << " failed: expected ";
  287. print( need );
  288. cerr << " received ";
  289. print( have );
  290. cerr << "." << endl;
  291. }
  292. }
  293.  
  294. int main( int argc, char* argv[] )
  295. {
  296.  
  297. JohnnysPhone objectJohnnysPhone;
  298.  
  299. //test case0
  300. vector <string> param00;
  301. param00.push_back("age messagd messagd message");
  302. string param01 = "message";
  303. int ret0 = objectJohnnysPhone.minimizePushes(param00,param01);
  304. int need0 = 8;
  305. assert_eq(0,ret0,need0);
  306.  
  307. //test case1
  308. vector <string> param10;
  309. param10.push_back("b a c d");
  310. string param11 = "abcd dcba ";
  311. int ret1 = objectJohnnysPhone.minimizePushes(param10,param11);
  312. int need1 = 23;
  313. assert_eq(1,ret1,need1);
  314.  
  315. //test case2
  316. vector <string> param20;
  317. param20.push_back("a b c");
  318. string param21 = "d";
  319. int ret2 = objectJohnnysPhone.minimizePushes(param20,param21);
  320. int need2 = -1;
  321. assert_eq(2,ret2,need2);
  322.  
  323. //test case3
  324. vector <string> param30;
  325. param30.push_back("gajdkwifpcks iclfabc");
  326. string param31 = "gajf";
  327. int ret3 = objectJohnnysPhone.minimizePushes(param30,param31);
  328. int need3 = -1;
  329. assert_eq(3,ret3,need3);
  330.  
  331. //test case4
  332. vector <string> param40;
  333. param40.push_back("a ");
  334. param40.push_back("aa ");
  335. param40.push_back("aaa ");
  336. param40.push_back("aaaa ");
  337. param40.push_back("ab");
  338. string param41 = "ab";
  339. int ret4 = objectJohnnysPhone.minimizePushes(param40,param41);
  340. int need4 = 5;
  341. assert_eq(4,ret4,need4);
  342.  
  343. //test case5
  344. vector <string> param50;
  345. param50.push_back("aaa ");
  346. param50.push_back("bbb ");
  347. param50.push_back("ccc");
  348. string param51 = "ccc";
  349. int ret5 = objectJohnnysPhone.minimizePushes(param50,param51);
  350. int need5 = 5;
  351. assert_eq(5,ret5,need5);
  352.  
  353. }
  354.