fork download
  1. // START BOILERPLATE HEADER
  2. // Useful Includes
  3. // <vector> <list> <map> <set> <deque> <queue> <stack> <bitset>
  4. // <algorithm> <functional> <numeric> <utility> <sstream> <iomanip>
  5. // <ctime> <cassert>
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <cstdio>
  10. #include <cmath>
  11. #include <cstring>
  12. #include <cstdlib>
  13.  
  14. using namespace std;
  15.  
  16. // Useful Constants
  17. #define PI 3.141592653589793238462643383279502884197169399375105820974944592307816406286
  18. #define E 2.718281828459045235360287471352662497757247093699959574966967627724076630353
  19.  
  20. //Useful Macros
  21. #define MAX(i,j) ((i)>(j)?(i):(j))
  22. #define MIN(i,j) ((i)<(j)?(i):(j))
  23. #define ABS(i) ((i)<0?-(i):(i))
  24. #define REP(i,a) for((i)=0;(i)<(a);(i)++)
  25. #define FOR(i,a,b) for((i)=a;(i)<(b);(i)++)
  26. #define FORE(i,a,b) for((i)=a;(i)<=(b);(i)++)
  27. #define EACH(it,b) for(__typeof((b).begin())it=(b).begin();it!=(b).end();it++)
  28. #define CLRI(ptr,n) memset((ptr),0,(n)*sizeof(int))
  29. #define CLRC(ptr,n) memset((ptr),0,(n)*sizeof(char))
  30.  
  31. //Useful Typedefs
  32. typedef long long ll;
  33. typedef unsigned long long ull;
  34.  
  35. //Fast stdin class (my windows g++ doesn't have getchar_unlocked, so this will have to do)
  36. class FastInput {
  37. private:
  38. static const int BUFSIZE = 1<<16;
  39. static char buffer[];
  40. char *bufpos;
  41. char *bufend;
  42. bool debug_mode;
  43. ifstream mystream;
  44. public:
  45. inline void getmore();
  46. inline void getint(int &n);
  47. inline void getull(ull &n);
  48. inline void getstr(char k[]);
  49. FastInput() { debug_mode = false; getmore(); }
  50. FastInput(const char *filename) { debug_mode = true; mystream.open(filename); getmore(); }
  51. inline void nextchar(char &c) { c = *bufpos++; if (bufpos==bufend) getmore(); }
  52. };
  53.  
  54. char FastInput::buffer[FastInput::BUFSIZE];
  55.  
  56. inline void FastInput::getmore() {
  57. bufpos = buffer;
  58. #ifdef ONLINE_JUDGE
  59. bufend = buffer + read(0, buffer, BUFSIZE);
  60. #else
  61. if (debug_mode) { mystream.read(buffer,BUFSIZE); bufend = buffer + mystream.gcount(); }
  62. else { cin.read(buffer,BUFSIZE); bufend = buffer + cin.gcount(); }
  63. #endif
  64. }
  65.  
  66. inline void FastInput::getint(int &n) {
  67. n = 0; register char ch; bool neg = false; nextchar(ch);
  68. while (ch < '0' || ch > '9') { if (ch=='-') neg = true; nextchar(ch); }
  69. while (ch >= '0' && ch <= '9') { n=(n<<3)+(n<<1)+ch-'0'; nextchar(ch); }
  70. if (neg) n = -n;
  71. }
  72.  
  73. inline void FastInput::getull(unsigned long long &n) {
  74. n = 0; register char ch; nextchar(ch);
  75. while (ch < '0' || ch > '9') { nextchar(ch); }
  76. while (ch >= '0' && ch <= '9') { n=(n<<3)+(n<<1)+ch-'0'; nextchar(ch); }
  77. }
  78.  
  79. inline void FastInput::getstr (char k[]) {
  80. register int cnt = 0; register char ch; nextchar(ch);
  81. while (( ch < 'a' || ch > 'z' ) && ( ch < 'A' || ch > 'Z' )) nextchar(ch);
  82. while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { k[cnt++] = ch; nextchar(ch); }
  83. k[cnt] = '\0';
  84. }
  85.  
  86.  
  87. class FastOutput {
  88. private:
  89. static const int BUFSIZE = 1<<16;
  90. static char buffer[];
  91. char *bufpos;
  92. char *bufend;
  93.  
  94. public:
  95. inline void flush();
  96. inline void putint(int n,char c);
  97. FastOutput() { bufpos = buffer; bufend = bufpos + BUFSIZE; }
  98. ~FastOutput() { flush(); }
  99. inline void wrchar(char c) { *bufpos++ = c; if (bufpos == bufend) flush(); }
  100. inline void putstr(char *k) { while (*k != 0) wrchar(*k++); }
  101. };
  102.  
  103. char FastOutput::buffer[FastOutput::BUFSIZE];
  104.  
  105. inline void FastOutput::flush() {
  106. if (bufpos == buffer) { return; }
  107. #ifdef ONLINE_JUDGE
  108. write(1, buffer, bufpos-buffer);
  109. #else
  110. cout.write(buffer,bufpos-buffer);
  111. #endif
  112. bufpos = buffer;
  113. }
  114.  
  115. inline void FastOutput::putint(int n, char c) {
  116. char buf[32]; bool neg = false; char *bufptr; bufptr = buf + 31; *bufptr-- = 0; if (c > 0) *bufptr-- = c;
  117. if (n == 0) *bufptr-- = '0';
  118. if (n < 0) { neg = true; n = -n; }
  119. while (n>0) { int r = n % 10; *bufptr-- = '0'+r; n /=10; }
  120. if (neg) { *bufptr-- = '-'; }
  121. putstr(++bufptr);
  122. }
  123.  
  124.  
  125.  
  126.  
  127. /////////////////////////////////////////////////////////////////////////////////////
  128. // SOLUTION STARTS HERE
  129. /////////////////////////////////////////////////////////////////////////////////////
  130.  
  131. char nextchar(FastInput &fi) {
  132. char c = 0;
  133. while ((c < 33) || (c > 126)) fi.nextchar(c);
  134. return c;
  135. }
  136.  
  137. int getpassstr(FastInput &fi, char *pass) {
  138. char c = 0; int i = 0;
  139. while ((c < 33) || (c > 126)) fi.nextchar(c);
  140. while ((c >= 33) && (c <= 126)) { pass[i++] = c; fi.nextchar(c); }
  141. return i;
  142. }
  143.  
  144. int main() {
  145. //FastInput fi;
  146. FastInput fi("FORGETPW.in");
  147. FastOutput fo;
  148. char a[128], pass[1000001];
  149. int i,j,k;
  150. int t; fi.getint(t);
  151. while (t--) {
  152. char s,t,ci;
  153. FOR(ci,0,127) { a[ci] = ci; }
  154. int n; fi.getint(n);
  155. while (n--) { s = nextchar(fi); t = nextchar(fi); a[s] = t; }
  156. int passlen = getpassstr(fi, pass);
  157. FOR(i,0,passlen) pass[i] = a[pass[i]];
  158. bool foundLeading = false, foundTrailing = false;
  159. // Do the stuff before the decimal
  160. FOR(i,0,passlen) {
  161. if (pass[i] == '.') break;
  162. if (!foundLeading && (pass[i] >= '1') && (pass[i] <= '9')) foundLeading = true;
  163. if (foundLeading) fo.wrchar(pass[i]);
  164. }
  165. // Check if we have a nonzero trailing decimal
  166. if (i < passlen-1) { for (j=passlen-1; j >= i; j--) { if ((pass[j] >= '1') && (pass[j] <= '9')) { foundTrailing = true; break; } } }
  167. if (foundTrailing) {
  168. fo.wrchar('.');
  169. FORE(k,i+1,j) fo.wrchar(pass[k]);
  170. }
  171. if (!foundLeading && !foundTrailing) { fo.wrchar('0'); }
  172. fo.wrchar('\n');
  173. fo.flush();
  174. }
  175. fo.flush();
  176. return(0);
  177. }
Success #stdin #stdout 0s 3836KB
stdin
1
0
001800
stdout
18001