Sicily 1194. Message Flood 排序+二分
这题本来很容易,由于自己看漏了个条件(大小写不敏感)WA了无数,还有用错了stl里的sort(参数传错了)
用stl里的sort排数组时, sort(begin, end, cmp),第二个参数要是数组最后元素的后一个的指针,当一个数组a有n个元素时,end= a+n(&a[n]),而不是&a[n-1]
#include <iostream> #include <string> #include <algorithm> #include <memory.h> using namespace std; bool cmp(string a, string b) { return a < b; } string name[20001], tmp; bool found[20001]; int n, m; bool bi_search(); int main() { int count; while (cin >> n && n) { cin >> m; memset(found, false, 20001*sizeof(bool)); for (int i = 0; i < n; i++) { cin >> name[i]; for (int j = 0; j < name[i].length(); j++) name[i][j] = tolower(name[i][j]); } count = n; sort(name, name+n, cmp); for (int i = 0; i < m; i++) { cin >> tmp; for (int j = 0; j < tmp.length(); j++) tmp[j] = tolower(tmp[j]); if (bi_search()) count--; } cout << count << endl; } return 0; } bool bi_search() { int start = 0, mid, end = n-1; while (start <= end) { mid = (start+end)/2; if (tmp == name[mid]) { if (!found[mid]) { found[mid] = true; return true; } else return false; } else if (tmp < name[mid]) end = mid-1; else start = mid+1; } return false; }