UVA-123 searching Quickly
problem ' s Link:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19296
Mean:
There is a string collection ignore, there is also a text collection txt, in the TXT in addition to the words in the ignore is the keyword, now you want to according to these keywords to the txt text to sort (according to the Dictionary of Keywords).
Note: The number of keywords in a line of txt text will need to be ranked in the order of the order, if the dictionary of the same keyword is sorted in sequence.
Analyse:
The problem of STL used more, using STL can be a good solution to the weight, sorting, such as a sequence of problems, and manual implementation of the words is a little cumbersome, the idea is not difficult.
Time Complexity:o (n)
Source Code:
1. STL Version:
/** This code was made by crazyacking* Problem:uva 123* verdict:accepted* submission date:2015-05-03-20.39* time:0ms * MEMORY:0KB*/#include<queue>#include<cstdio>#include<string>#include<stack>#include<cmath>#include<Set>#include<map>#include<cstdlib>#include<climits>#include<vector>#include<iostream>#include<algorithm>#include<cstring>#defineMAXN 1000010#defineLL Long Long#defineULL unsigned long Longusing namespacestd;stringtmp;Set<string>Ignore;multimap<string,string>MP;intMain () {//freopen ("C:\\users\\crazyacking\\desktop\\cin.txt", "R", stdin);//freopen ("C:\\users\\crazyacking\\desktop\\cout.txt", "w", stdout);Ios_base::sync_with_stdio (false); Cin.tie (0); intLen; stringBig; while(Getline (cin,ig) && ig!="::") Ignore.insert (IG); Mp.clear (); while(Getline (cin,tmp)) {len=tmp.length (); for(intI=0; i<len;++i) tmp[i]=ToLower (Tmp[i]); stringT1; intCNT; for(intI=0; i<len;++i) {if(tmp[i]!=' ') {CNT=0; intJ; T1.clear (); for(j=i;j<len;++j) {if(tmp[j]!=' ') {T1.insert (CNT,1, Tmp[j]); CNT++; TMP[J]=ToUpper (Tmp[j]); } Else Break; } I=J; if(Ignore.find (T1) = =ignore.end ()) Mp.insert (pair<string,string>(t1,tmp)); for(j=0; j<len;++j) {Tmp[j]=ToLower (Tmp[j]); } }}} Multimap<string,string>:: Iterator iter=Mp.begin (); for(; Iter!=mp.end (); + +ITER) {cout<< (Iter->second) <<Endl; } return 0;}/**/View Code
2. Manual simulation:
/** This code was made by crazyacking* verdict:accepted* submission date:2015-05-03-21.52* time:0ms* memory:1347kb*/#include<queue>#include<cstdio>#include<string>#include<stack>#include<cmath>#include<Set>#include<map>#include<cstdlib>#include<climits>#include<vector>#include<iostream>#include<algorithm>#include<cstring>#defineMAXN 1000010#defineLL Long Longusing namespacestd;structnode{intN; Charword[ -][ the]; voidFunChar*str) { intlen=strlen (str); for(intI=0; i<len;i++) str[i]=ToLower (Str[i]); N=0; Char*strptr=strtok (str," "); while(strptr!=NULL) {strcpy (Word[n++],strptr); StrPtr=strtok (NULL," "); }}}title[ -];voidOutputintTintPOS) { intlen=strlen (Title[t].word[pos]); for(intI=0; i<len;i++) {Title[t].word[pos][i]=ToUpper (Title[t].word[pos][i]); } for(intI=0; i<title[t].n;i++) { if(i==0) {printf ("%s", Title[t].word[i]); } Elseprintf"%s", Title[t].word[i]); } for(intI=0; i<len;i++) {Title[t].word[pos][i]=ToLower (Title[t].word[pos][i]); } puts ("");}intMain () {Ios_base::sync_with_stdio (false); Cin.tie (0); intn=0; Set<string>Ig,key; Set<string>:: Iterator it; Chartemp[ -],str[10005]; while(SCANF ("%s", temp)! =EOF) { if(strcmp (temp,"::")==0) Break; Ig.insert (temp); } while(gets (str)) {title[n].fun (str); for(intI=0; i<title[n].n;i++) { BOOLflag=false; for(It=ig.begin (); It!=ig.end (); it++) { if(strcmp (Title[n].word[i], (*it). C_STR ()) = =0) {flag=true; Break; } } if(!flag) Key.insert (Title[n].word[i]); } N++; } for(It=key.begin (); It!=key.end (); it++) { for(intI=0; i<n;i++) { for(intj=0; j<title[i].n;j++) { if(strcmp (*it). C_str (), title[i].word[j]) = =0) {output (I,J); } } } } return 0;}/**/View Code
STL---UVA 123 searching Quickly