123 - Searching Quickly

来源:互联网 发布:淘宝模板化设置 编辑:程序博客网 时间:2024/05/21 22:30
#include <iostream>#include <string>#include <vector>#include <list>#include <cstring>using namespace std;struct comp {string title;string keyword;};bool has_ele ( vector<string> arr, string target ) {for ( auto e : arr ) {if ( e == target )return true;}return false;}bool foo1 ( const comp & lhs, const comp & rhs ) {return lhs.keyword < rhs.keyword;}int main ( int argc, char * argv[] ) {vector<string> vec_ign, vec_tit;list<comp> list_comp;string tmp_s;while ( cin >> tmp_s ) {if ( tmp_s != "::" ) {for ( auto & c : tmp_s ) c = tolower ( c );vec_ign.push_back ( tmp_s );} else {break;}}while ( getline ( cin, tmp_s ) ) {for ( auto & c : tmp_s ) c = tolower ( c );vec_tit.push_back ( tmp_s );}/* pre-process */for ( auto v : vec_tit ) {const char * ptr = v.c_str();int len = strlen ( ptr );int last = 0;int i, j;for ( i = 0; i < len; i++ ) {if ( ptr[ i ] == ' ' || i == len - 1 ) {int start = last, end = i - 1;if ( ptr[ i ] != ' ' && i == len - 1 ) end = i;char word[64];for ( j = start; j <= end; j++ ) word[ j - start ] = ptr [ j ];word [ end -start + 1 ] = '\0';tmp_s = word;if ( ! has_ele ( vec_ign, tmp_s ) ) {comp c1;c1.keyword = tmp_s;c1.title = v;for ( j = start; j <= end; j++ ) c1.title[ j ] = toupper ( c1.title[ j ] );list_comp.push_back ( c1 );}last = i + 1;}}}list_comp.sort ( foo1 );for ( auto l : list_comp ) {cout << l.title << endl;}return 0;}

0 0
原创粉丝点击