Problem
Given a dictionary, ask again.
There is a blank line between the dictionary and the query.
Cin.peek () is a pointer to the current character.
#include <iostream> #include <string> #include <map>using namespace std;map<string, String>dic ; string s, T;int f;int main () { ios::sync_with_stdio (false); while (CIN >> s) if (cin.peek () = = ' \ n ')//The pointer currently points to \ n if (!f) { f = 1; Dic[s] = t;//The current read in S is alien, the t just read into is the corresponding English } else cout << ((t = dic[s])! = ""? T: "eh") << "\ n"; else//Description S is a dictionary of English { F = 0;//f=0 represents the next to be read outside the star t = S; }}
Tips for handling empty lines
#include <cstdio> #include <algorithm> #include <cstring>using namespace std; #define N 100005#define M 15char s[n];int c;struct mydic{ char zh[m];//outer star char en[m];//English} dic[n];int cmp (const mydic &a, const Mydic & AMP;B) { return strcmp (A.zh, B.zh) >= 0;} void Solve () { int l = 0, r = C; while (L < R) { int m = r+l >> 1; int f = strcmp (S, Dic[m].zh); if (f = = 0) { printf ("%s\n", dic[m].en); return; } if (F < 0) L = m + 1; else r = m; } printf ("eh\n");} int main () {while (gets (s)) { if (!s[0]) break;//read to the empty line sscanf (S, "%s%s", Dic[c].en,dic[c].zh); C + +; } Sort (dic, DIC + C, CMP); while (gets (s)) Solve ();}
"POJ 2503" Babelfish (String)