fork download
  1. // Name:- Kunal Sheth
  2.  
  3. //just a half-hearted try...lets hope for the best!!!
  4.  
  5. //header files
  6.  
  7. #include<iostream>
  8. #include<cstdio>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<map>
  12. #include<cmath>
  13. #include<cstring>
  14. using namespace std;
  15.  
  16. //end of header files
  17.  
  18.  
  19. //definitions
  20.  
  21. #define MOD 1000000007
  22. #define llu long long unsigned
  23. #define lld long long
  24. #define ld long
  25. #define getchar_unlocked getchar
  26. #define putchar_unlocked putchar
  27. #define eps 0.0000001
  28. //end of definitions
  29.  
  30.  
  31. //fast input
  32.  
  33. int scan_d() {int ip=getchar_unlocked(),ret=0,flag=1;for(;ip<'0'||ip>'9';ip=getchar_unlocked())if(ip=='-'){flag=-1;ip=getchar_unlocked();break;}for(;ip>='0'&&ip<='9';ip=getchar_unlocked())ret=ret*10+ip-'0';return flag*ret;}
  34. ld scan_ld() {int ip=getchar_unlocked(),flag=1;ld ret=0;for(;ip<'0'||ip>'9';ip=getchar_unlocked())if(ip=='-'){flag=-1;ip=getchar_unlocked();break;}for(;ip>='0'&&ip<='9';ip=getchar_unlocked())ret=ret*10+ip-'0';return flag*ret;}
  35. lld scan_lld() {int ip=getchar_unlocked(),flag=1;lld ret=0;for(;ip<'0'||ip>'9';ip=getchar_unlocked())if(ip=='-'){flag=-1;ip=getchar_unlocked();break;}for(;ip>='0'&&ip<='9';ip=getchar_unlocked())ret=ret*10+ip-'0';return flag*ret;}
  36. llu scan_llu() {int ip=getchar_unlocked();llu ret=0;for(;ip<'0'||ip>'9';ip=getchar_unlocked());for(;ip>='0'&&ip<='9';ip=getchar_unlocked())ret=ret*10+ip-'0';return ret;}
  37.  
  38. //end of fast input
  39.  
  40. //fast output
  41.  
  42. //no line break
  43. void print_d(int n) {if(n<0){n=-n;putchar_unlocked('-');}int i=10;char output_buffer[10];do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<10);}
  44. void print_ld(ld n) {if(n<0){n=-n;putchar_unlocked('-');}int i=11;char output_buffer[11];do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<11);}
  45. void print_lld(lld n) {if(n<0){n=-n;putchar_unlocked('-');}int i=21;char output_buffer[21];do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<21);}
  46. void print_llu(llu n) {int i=21;char output_buffer[21];do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<21);}
  47.  
  48. //new line
  49. void println_d(int n) {if(n<0){n=-n;putchar_unlocked('-');}int i=10;char output_buffer[11];output_buffer[10]='\n';do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<11);}
  50. void println_ld(ld n) {if(n<0){n=-n;putchar_unlocked('-');}int i=11;char output_buffer[12];output_buffer[11]='\n';do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<12);}
  51. void println_lld(lld n) {if(n<0){n=-n;putchar_unlocked('-');}int i=21;char output_buffer[22];output_buffer[21]='\n';do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<22);}
  52. void println_llu(llu n) {int i=21;char output_buffer[22];output_buffer[21]='\n';do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<22);}
  53.  
  54. //special char
  55. char sp;
  56. void printsp_d(int n) {if(n<0){n=-n;putchar_unlocked('-');}int i=10;char output_buffer[11];output_buffer[10]=sp;do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<11);}
  57. void printsp_ld(ld n) {if(n<0){n=-n;putchar_unlocked('-');}int i=11;char output_buffer[12];output_buffer[11]=sp;do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<12);}
  58. void printsp_lld(lld n) {if(n<0){n=-n;putchar_unlocked('-');}int i=21;char output_buffer[22];output_buffer[21]=sp;do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<22);}
  59. void printsp_llu(llu n) {int i=21;char output_buffer[22];output_buffer[21]=sp;do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{putchar_unlocked(output_buffer[i]);}while(++i<22);}
  60.  
  61. //end of fast output
  62.  
  63. double dp[2][26];
  64. double arr[26][26];
  65. double probs[3][26];
  66. string s;
  67. int ln;
  68. int k;
  69.  
  70. //extra functions
  71.  
  72. string get()
  73. {
  74. string ret="";
  75. char temp=getchar_unlocked();
  76. while(temp<'a'||temp>'z')
  77. temp=getchar_unlocked();
  78. while(temp>='a'&&temp<='z')
  79. {
  80. ret+=temp;
  81. temp=getchar_unlocked();
  82. }
  83. return ret;
  84. }
  85.  
  86. void doit()
  87. {
  88. for(int q=0;q<ln;q++)
  89. {
  90. for(int w=0;w<26;w++)
  91. dp[0][w]=dp[1][w]=0;
  92. dp[0][s[q]-'a']=1;
  93. for(int w=0;w<k;w++)
  94. {
  95. for(int i=0;i<26;i++)
  96. {
  97. for(int j=0;j<26;j++)
  98. {
  99. dp[1][i]+=dp[0][j]*arr[j][i];
  100. }
  101. }
  102. bool is=1;
  103. for(int i=0;i<26;i++)
  104. {
  105. //cout<<dp[1][i]<<" ";
  106. if(dp[0][i]-dp[1][i]>eps)
  107. is=0;
  108. dp[0][i]=dp[1][i];
  109. dp[1][i]=0;
  110. }
  111. if(is)
  112. {
  113. cout<<"Iters: "<<w<<endl;
  114. //cout<<"hua baba...hua\n";
  115. break;
  116. }
  117. //cout<<endl;
  118. }
  119. for(int w=0;w<26;w++)
  120. probs[q][w]=dp[0][w];
  121. }
  122. return;
  123. }
  124.  
  125. //end of extra fnctions
  126.  
  127.  
  128. //main
  129.  
  130. map<string,bool>mp;
  131.  
  132. int main()
  133. {
  134. int t=scan_d();
  135. while(t--)
  136. {
  137. int n=scan_d();
  138. k=scan_d();
  139. s=get();
  140. ln=s.length();
  141. for(int i=0;i<26;i++)
  142. for(int j=0;j<26;j++)
  143. cin>>arr[i][j];
  144. doit();
  145. double ans=0;
  146. while(n--)
  147. {
  148. string tp=get();
  149. int tpln=tp.length();
  150. if(!mp[tp]&&tpln==ln)
  151. {
  152. mp[tp]=1;
  153. double tans=1;
  154. for(int i=0;i<ln;i++)
  155. tans*=probs[i][tp[i]-'a'];
  156. ans+=tans;
  157. }
  158. }
  159. printf("%7lf\n",ans);
  160. mp.clear();
  161. }
  162. return 0;
  163. }
  164.  
  165. //end of main
  166.  
Success #stdin #stdout 2.07s 3444KB
stdin
3
1 1000000000
a
0.99999 0.00001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0.00001 0.99999 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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