123 - Searching Quickly

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

User avatar
kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:

Re: #123, If you get AC, please try this input.

Post by kbr_iut » Fri Jan 09, 2009 2:35 pm

I am passing all of the tests i tried.can anyone tell my why I am getting WA.

Code: Select all

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
struct st{vector<string>vc;string key;};
vector<struct st>v;
vector<string>in;
string a,b,c;
int wn,tn,kn;
bool comp(const struct st &p,const struct st &q){
	return p.key<q.key;
}
int main(){
	//freopen("1.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int i,j,k,l,m,n;
	while(cin>>a){
		if(a=="::")break;
		in.push_back(a);
	}
	getline(cin,a);
	while(getline(cin,a)){
		for(i=0;i<a.size();i++)if(a[i]>='A'&&a[i]<='Z')a[i]+=32;
		istringstream istr(a);
		struct st var;
		while(istr>>b){
			var.vc.push_back(b);
		}
		for(i=0;i<var.vc.size();i++){
			c=var.vc[i];
			if(find(in.begin(),in.end(),c)==in.end()){
				for(j=0;j<c.size();j++)if(c[j]>='a'&&c[j]<='z')c[j]-=32;
				struct st sv=var;
				sv.key=c;
				sv.vc[i]=c;
				v.push_back(sv);
			}
		}
	}
	sort(v.begin(),v.end(),comp);
	for(i=0;i<v.size();i++){
		for(j=0;j<v[i].vc.size()-1;j++)cout<<v[i].vc[j]<<" ";
		cout<<v[i].vc[j]<<endl;
	}
	return 0;
}


thanx in advance.
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................

sefakilic
New poster
Posts: 7
Joined: Wed Mar 11, 2009 8:12 pm

Re: 123 Searching Quickly

Post by sefakilic » Thu Aug 06, 2009 5:45 pm

Hello, I am getting WA for the problem.
Here is my code, i can't find the error.

Code: Select all

#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <vector>
using namespace std;
string ignores[50];
string titles[200];
struct x {
    string potential;
    int index;
    int indexintitle;
}; 
vector<struct x> potentials;
vector<string> titlewords;

string toLower(string x)
{
    // assuming string x contains only [A-Z] and [a-z]
    int len = x.length();
    for(int i = 0; i < len; ++i)
    {
        if(x[i] >= 'A' && x[i] <= 'Z')
            x[i] = x[i] + 32;
    }
    return x;
}

string toUpper(string x)
{
    // assuming string x contains only [A-Z] and [a-z]
    int len = x.length();
    for(int i = 0; i < len; ++i)
    {
        if(x[i] >= 'a' && x[i] <= 'z')
            x[i] = x[i] - 32;
    }
    return x;
}


bool cmp (struct x a, struct x b)
{
    if(toLower(a.potential) != toLower(b.potential))
        return (toLower(a.potential) < toLower(b.potential));
    else return (a.index < b.index);
}
int main()
{
    string tmp;
    int numberignores = 0;
    int numbertitles = 0;
    stringstream ss;
    
    while(cin >> tmp)
    {
        if(tmp == "::") break;
        else ignores[numberignores++] = tmp;
    }
    sort(ignores, ignores + numberignores);
    while(getline(cin, tmp))
    {
        if(tmp.length() == 0) continue;
        titles[numbertitles++] = tmp;
    }
    potentials.clear();
    for(int i = 0; i < numbertitles; ++i)
    {
        //cout << "|" << titles[i] << "|" << endl;
        titlewords.clear();
        ss.clear();
        ss.str(titles[i]);
        while(ss >> tmp)
            titlewords.push_back(tmp);

        for(vector<string>::iterator it = titlewords.begin(); it != titlewords.end(); ++it)
        {
            //cout << "x: " << *it << " " << toLower(*it) << endl;
            if(binary_search(ignores, ignores + numberignores, toLower(*it)) == false)
            {
                //cout << "potential: " << *it << endl;
                struct x tmpx;
                tmpx.potential = *it;
                tmpx.index     = i;
                tmpx.indexintitle = it - titlewords.begin();
                potentials.push_back(tmpx);
            }
        }
       
    }
    sort(potentials.begin(), potentials.end(), cmp);
   
    for(vector<struct x>::iterator it = potentials.begin(); it != potentials.end(); ++it)
    {
        struct x tmpx = *it;
        ss.clear();
        ss.str(titles[tmpx.index]);
        bool first = true;
        int wordcount  = 0; 
        while(ss >> tmp)
        {
            wordcount += 1;
            if(first) { first = false; }
            else cout << " ";
            if(tmp == tmpx.potential && tmpx.indexintitle + 1 == wordcount) cout << toUpper(tmp);
            else cout << toLower(tmp);
        }
        cout << endl;
    }
    
    return 0;
}
    

User avatar
Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria

Re: 123 Searching Quickly

Post by Sedefcho » Wed Aug 12, 2009 10:47 pm

Here is a wikipedia article which was changed a little
bit in order to match this problem's requirements.

INPUT

Code: Select all

the
for
on
in
and
is
it
american
canadian
great
lakes
::
The Niagara Falls are voluminous waterfalls on the Niagara River
straddling the international border between the Canadian province
of Ontario and the U S  state of New York  The falls are
N miles BBBBBBSSSM kmBBBBBBSSS north northwest of Buffalo New York and
S miles BBBBBBSSSZ kmBBBBBBSSS south southeast of Toronto Ontario
between the twin cities of Niagara Falls Ontario
and Niagara Falls New York  Niagara Falls is composed
of two major sections separated by Goat Island:
Horseshoe Falls the majority of which lies on the
Canadian side of the border and American Falls on the
American side  The smaller Bridal Veil Falls are also
located on the American side separated from the main falls
by Luna Island  Niagara Falls were formed when glaciers
receded at the end of the Wisconsin glaciation BBBBBBSSSthe last ice ageBBBBBBSSS
and water from the newly formed Great Lakes carved a path through
the Niagara Escarpment en route to the Atlantic Ocean
While not exceptionally high the Niagara Falls are very wide
More than six million cubic feet BBBBBBSSSXYZKLM mSBBBBBBSSS of water falls
over the crest line every minute in high flow and almost
F million cubic feet BBBBBBSSSXYZKLM mSBBBBBBSSS on average  It is the most
powerful waterfall in North America

The Niagara Falls are renowned both for their beauty and
as a valuable source of hydroelectric power  Managing the
balance between recreational commercial and industrial uses
has been a challenge for the stewards of the falls since the TKLMs

OUTPUT

Code: Select all

and water from the newly formed great lakes carved A path through
as A valuable source of hydroelectric power  managing the
has been A challenge for the stewards of the falls since the tklms
receded at the end of the wisconsin glaciation bbbbbbsssthe last ice AGEBBBBBBSSS
over the crest line every minute in high flow and ALMOST
american side  the smaller bridal veil falls are ALSO
powerful waterfall in north AMERICA
the niagara falls ARE voluminous waterfalls on the niagara river
of ontario and the u s  state of new york  the falls ARE
american side  the smaller bridal veil falls ARE also
while not exceptionally high the niagara falls ARE very wide
the niagara falls ARE renowned both for their beauty and
AS a valuable source of hydroelectric power  managing the
receded AT the end of the wisconsin glaciation bbbbbbsssthe last ice agebbbbbbsss
the niagara escarpment en route to the ATLANTIC ocean
f million cubic feet bbbbbbsssxyzklm msbbbbbbsss on AVERAGE  it is the most
BALANCE between recreational commercial and industrial uses
n miles BBBBBBSSSM kmbbbbbbsss north northwest of buffalo new york and
receded at the end of the wisconsin glaciation BBBBBBSSSTHE last ice agebbbbbbsss
more than six million cubic feet BBBBBBSSSXYZKLM msbbbbbbsss of water falls
f million cubic feet BBBBBBSSSXYZKLM msbbbbbbsss on average  it is the most
s miles BBBBBBSSSZ kmbbbbbbsss south southeast of toronto ontario
the niagara falls are renowned both for their BEAUTY and
has BEEN a challenge for the stewards of the falls since the tklms
straddling the international border BETWEEN the canadian province
BETWEEN the twin cities of niagara falls ontario
balance BETWEEN recreational commercial and industrial uses
straddling the international BORDER between the canadian province
canadian side of the BORDER and american falls on the
the niagara falls are renowned BOTH for their beauty and
american side  the smaller BRIDAL veil falls are also
n miles bbbbbbsssm kmbbbbbbsss north northwest of BUFFALO new york and
of two major sections separated BY goat island:
BY luna island  niagara falls were formed when glaciers
and water from the newly formed great lakes CARVED a path through
has been a CHALLENGE for the stewards of the falls since the tklms
between the twin CITIES of niagara falls ontario
balance between recreational COMMERCIAL and industrial uses
and niagara falls new york  niagara falls is COMPOSED
over the CREST line every minute in high flow and almost
more than six million CUBIC feet bbbbbbsssxyzklm msbbbbbbsss of water falls
f million CUBIC feet bbbbbbsssxyzklm msbbbbbbsss on average  it is the most
the niagara escarpment EN route to the atlantic ocean
receded at the END of the wisconsin glaciation bbbbbbsssthe last ice agebbbbbbsss
the niagara ESCARPMENT en route to the atlantic ocean
over the crest line EVERY minute in high flow and almost
while not EXCEPTIONALLY high the niagara falls are very wide
F million cubic feet bbbbbbsssxyzklm msbbbbbbsss on average  it is the most
the niagara FALLS are voluminous waterfalls on the niagara river
of ontario and the u s  state of new york  the FALLS are
between the twin cities of niagara FALLS ontario
and niagara FALLS new york  niagara falls is composed
and niagara falls new york  niagara FALLS is composed
horseshoe FALLS the majority of which lies on the
canadian side of the border and american FALLS on the
american side  the smaller bridal veil FALLS are also
located on the american side separated from the main FALLS
by luna island  niagara FALLS were formed when glaciers
while not exceptionally high the niagara FALLS are very wide
more than six million cubic feet bbbbbbsssxyzklm msbbbbbbsss of water FALLS
the niagara FALLS are renowned both for their beauty and
has been a challenge for the stewards of the FALLS since the tklms
more than six million cubic FEET bbbbbbsssxyzklm msbbbbbbsss of water falls
f million cubic FEET bbbbbbsssxyzklm msbbbbbbsss on average  it is the most
over the crest line every minute in high FLOW and almost
by luna island  niagara falls were FORMED when glaciers
and water from the newly FORMED great lakes carved a path through
located on the american side separated FROM the main falls
and water FROM the newly formed great lakes carved a path through
receded at the end of the wisconsin GLACIATION bbbbbbsssthe last ice agebbbbbbsss
by luna island  niagara falls were formed when GLACIERS
of two major sections separated by GOAT island:
HAS been a challenge for the stewards of the falls since the tklms
while not exceptionally HIGH the niagara falls are very wide
over the crest line every minute in HIGH flow and almost
HORSESHOE falls the majority of which lies on the
as a valuable source of HYDROELECTRIC power  managing the
receded at the end of the wisconsin glaciation bbbbbbsssthe last ICE agebbbbbbsss
balance between recreational commercial and INDUSTRIAL uses
straddling the INTERNATIONAL border between the canadian province
by luna ISLAND  niagara falls were formed when glaciers
of two major sections separated by goat ISLAND:
n miles bbbbbbsssm KMBBBBBBSSS north northwest of buffalo new york and
s miles bbbbbbsssz KMBBBBBBSSS south southeast of toronto ontario
receded at the end of the wisconsin glaciation bbbbbbsssthe LAST ice agebbbbbbsss
horseshoe falls the majority of which LIES on the
over the crest LINE every minute in high flow and almost
LOCATED on the american side separated from the main falls
by LUNA island  niagara falls were formed when glaciers
located on the american side separated from the MAIN falls
of two MAJOR sections separated by goat island:
horseshoe falls the MAJORITY of which lies on the
as a valuable source of hydroelectric power  MANAGING the
n MILES bbbbbbsssm kmbbbbbbsss north northwest of buffalo new york and
s MILES bbbbbbsssz kmbbbbbbsss south southeast of toronto ontario
more than six MILLION cubic feet bbbbbbsssxyzklm msbbbbbbsss of water falls
f MILLION cubic feet bbbbbbsssxyzklm msbbbbbbsss on average  it is the most
over the crest line every MINUTE in high flow and almost
MORE than six million cubic feet bbbbbbsssxyzklm msbbbbbbsss of water falls
f million cubic feet bbbbbbsssxyzklm msbbbbbbsss on average  it is the MOST
more than six million cubic feet bbbbbbsssxyzklm MSBBBBBBSSS of water falls
f million cubic feet bbbbbbsssxyzklm MSBBBBBBSSS on average  it is the most
N miles bbbbbbsssm kmbbbbbbsss north northwest of buffalo new york and
of ontario and the u s  state of NEW york  the falls are
n miles bbbbbbsssm kmbbbbbbsss north northwest of buffalo NEW york and
and niagara falls NEW york  niagara falls is composed
and water from the NEWLY formed great lakes carved a path through
the NIAGARA falls are voluminous waterfalls on the niagara river
the niagara falls are voluminous waterfalls on the NIAGARA river
between the twin cities of NIAGARA falls ontario
and NIAGARA falls new york  niagara falls is composed
and niagara falls new york  NIAGARA falls is composed
by luna island  NIAGARA falls were formed when glaciers
the NIAGARA escarpment en route to the atlantic ocean
while not exceptionally high the NIAGARA falls are very wide
the NIAGARA falls are renowned both for their beauty and
n miles bbbbbbsssm kmbbbbbbsss NORTH northwest of buffalo new york and
powerful waterfall in NORTH america
n miles bbbbbbsssm kmbbbbbbsss north NORTHWEST of buffalo new york and
while NOT exceptionally high the niagara falls are very wide
the niagara escarpment en route to the atlantic OCEAN
OF ontario and the u s  state of new york  the falls are
of ontario and the u s  state OF new york  the falls are
n miles bbbbbbsssm kmbbbbbbsss north northwest OF buffalo new york and
s miles bbbbbbsssz kmbbbbbbsss south southeast OF toronto ontario
between the twin cities OF niagara falls ontario
OF two major sections separated by goat island:
horseshoe falls the majority OF which lies on the
canadian side OF the border and american falls on the
receded at the end OF the wisconsin glaciation bbbbbbsssthe last ice agebbbbbbsss
more than six million cubic feet bbbbbbsssxyzklm msbbbbbbsss OF water falls
as a valuable source OF hydroelectric power  managing the
has been a challenge for the stewards OF the falls since the tklms
of ONTARIO and the u s  state of new york  the falls are
s miles bbbbbbsssz kmbbbbbbsss south southeast of toronto ONTARIO
between the twin cities of niagara falls ONTARIO
OVER the crest line every minute in high flow and almost
and water from the newly formed great lakes carved a PATH through
as a valuable source of hydroelectric POWER  managing the
POWERFUL waterfall in north america
straddling the international border between the canadian PROVINCE
RECEDED at the end of the wisconsin glaciation bbbbbbsssthe last ice agebbbbbbsss
balance between RECREATIONAL commercial and industrial uses
the niagara falls are RENOWNED both for their beauty and
the niagara falls are voluminous waterfalls on the niagara RIVER
the niagara escarpment en ROUTE to the atlantic ocean
of ontario and the u S  state of new york  the falls are
S miles bbbbbbsssz kmbbbbbbsss south southeast of toronto ontario
of two major SECTIONS separated by goat island:
of two major sections SEPARATED by goat island:
located on the american side SEPARATED from the main falls
canadian SIDE of the border and american falls on the
american SIDE  the smaller bridal veil falls are also
located on the american SIDE separated from the main falls
has been a challenge for the stewards of the falls SINCE the tklms
more than SIX million cubic feet bbbbbbsssxyzklm msbbbbbbsss of water falls
american side  the SMALLER bridal veil falls are also
as a valuable SOURCE of hydroelectric power  managing the
s miles bbbbbbsssz kmbbbbbbsss SOUTH southeast of toronto ontario
s miles bbbbbbsssz kmbbbbbbsss south SOUTHEAST of toronto ontario
of ontario and the u s  STATE of new york  the falls are
has been a challenge for the STEWARDS of the falls since the tklms
STRADDLING the international border between the canadian province
more THAN six million cubic feet bbbbbbsssxyzklm msbbbbbbsss of water falls
the niagara falls are renowned both for THEIR beauty and
and water from the newly formed great lakes carved a path THROUGH
has been a challenge for the stewards of the falls since the TKLMS
the niagara escarpment en route TO the atlantic ocean
s miles bbbbbbsssz kmbbbbbbsss south southeast of TORONTO ontario
between the TWIN cities of niagara falls ontario
of TWO major sections separated by goat island:
of ontario and the U s  state of new york  the falls are
balance between recreational commercial and industrial USES
as a VALUABLE source of hydroelectric power  managing the
american side  the smaller bridal VEIL falls are also
while not exceptionally high the niagara falls are VERY wide
the niagara falls are VOLUMINOUS waterfalls on the niagara river
and WATER from the newly formed great lakes carved a path through
more than six million cubic feet bbbbbbsssxyzklm msbbbbbbsss of WATER falls
powerful WATERFALL in north america
the niagara falls are voluminous WATERFALLS on the niagara river
by luna island  niagara falls WERE formed when glaciers
by luna island  niagara falls were formed WHEN glaciers
horseshoe falls the majority of WHICH lies on the
WHILE not exceptionally high the niagara falls are very wide
while not exceptionally high the niagara falls are very WIDE
receded at the end of the WISCONSIN glaciation bbbbbbsssthe last ice agebbbbbbsss
of ontario and the u s  state of new YORK  the falls are
n miles bbbbbbsssm kmbbbbbbsss north northwest of buffalo new YORK and
and niagara falls new YORK  niagara falls is composed

at5anpo3
New poster
Posts: 7
Joined: Sun Apr 29, 2012 6:46 pm

Re: 123 Searching Quickly

Post by at5anpo3 » Thu May 10, 2012 2:44 pm

The example above should be missing ":" from

Code: Select all

by Goat Island:
The output file MUST end with a \n, or else you fail the judge! (had to find it the hard way).

Best Regards,
at5anpo3

czjxy881
New poster
Posts: 1
Joined: Mon Jul 02, 2012 12:56 pm

123 searching quickly who could help??

Post by czjxy881 » Mon Jul 02, 2012 1:02 pm

my code gets a WA at every time,who could tell me where is wrong?
:cry: :cry:

Code: Select all

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string.h>
using namespace std;
char ignore[3001][200]={0};
char line[500][1000]={0};
bool ig[3001]={0};
int n=0;
int m=0;


void find(char a[1000])
{
    int i,j,k;
    for(i=0;i<strlen(a);i++)
    {
        char t[200]={0};
        if(sscanf(a+i,"%s",t)==0)break;
        //cout<<t<<endl;
        int len=strlen(t);
        i+=len;
        for(j=0;j<n&&strcmp(ignore[j],t)==-1;j++);
        if(j==n||strcmp(ignore[j],t)==1)
        {
            for(k=n-1;k>=j;k--)
            {
               strcpy(ignore[k+1],ignore[k]);
               ig[k+1]=ig[k];
            }
            strcpy(ignore[j],t);
            ig[j]=1;
            n++;
        }
        else continue;
    }
    return;
}

void output()
{
    int i,j,k;
    for(i=0;i<n;i++)
    {
        //cout<<ignore[i]<<" "<<ig[i]<<endl;
        if(ig[i]!=1)continue;
        else
        {
            char t[200]={'\0'};
            for(j=0;j<strlen(ignore[i]);j++)
            {
                t[j]=toupper(ignore[i][j]);
            }
            for(j=0;j<m;j++)
            {
                for(k=0;k<strlen(line[j]);k++)
                {
                    char *p=strstr(line[j]+k,ignore[i]);
                    if(p==NULL) break;
                    else
                    {
                        char t1=*p;
                        *p='\0';
                        k=p-line[j]+strlen(t);
                        if((p==line[j]||*(p-1)==' ')&&(line[j][k]==' '||line[j][k]=='\0'))
                           printf("%s%s%s\n",line[j],t,p+strlen(t));
                        k--;
                        *p=t1;
                    }
                }
            }
        }
    }
    return;
}

void input()
{
    while(1)
    {
        char t[1000]={0};
        scanf("%s",t);
        while(getchar()!='\n');
        if(t[0]==':')break;
        int i,k;
        for(i=0;i<strlen(t);i++)
          t[i]=tolower(t[i]);
        for(i=0;i<n&&strcmp(ignore[i],t)==-1;i++);
        if(i==n||strcmp(ignore[i],t)==1)
        {
            for(k=n-1;k>=i;k--)
            {
               strcpy(ignore[k+1],ignore[k]);
               ig[k+1]=ig[k];
            }
            strcpy(ignore[i],t);
            ig[i]=0;
            n++;
        }
        memset(t,0,sizeof(t));
    }
    while(gets(line[m])!=NULL) :cry: 
    {
        int i;
        for(i=0;i<strlen(line[m]);i++)
           line[m][i]=tolower(line[m][i]);
        find(line[m]);
        m++;
    }
    return;
}


int main()
{
    //freopen("out.txt","w",stdout);
    input();
    output();
    //cout<<endl;
    return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 123 searching quickly who could help??

Post by brianfry713 » Mon Jul 02, 2012 9:21 pm

Line 102,

Code: Select all

    while(gets(line[m])!=NULL) :cry:
doesn't compile.
Check input and AC output for thousands of problems on uDebug!

ccu499410030
New poster
Posts: 3
Joined: Fri Aug 31, 2012 9:25 pm

123 Wrong Answer

Post by ccu499410030 » Sat Sep 01, 2012 6:14 am

AC
Last edited by ccu499410030 on Sun Mar 24, 2013 4:33 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 123 Wrong Answer

Post by brianfry713 » Tue Sep 04, 2012 10:36 pm

ig_count becomes larger than 200 during the execution of your code.
Check input and AC output for thousands of problems on uDebug!

ccu499410030
New poster
Posts: 3
Joined: Fri Aug 31, 2012 9:25 pm

Re: 123 Wrong Answer

Post by ccu499410030 » Wed Sep 05, 2012 5:46 pm

I thought 200 is big enough,
guess you're right!
Thanks alot.
AC finally~

hello
New poster
Posts: 25
Joined: Sun Mar 10, 2013 7:29 pm

123 - 123 - Searching Quickly

Post by hello » Tue May 20, 2014 4:40 am

Why WA ....

Code: Select all

#include <bits/stdc++.h>

using namespace std;

#define zero(arr) memset(arr,0,sizeof(arr));
#define mem(arr,k) memset(arr,k,sizeof(arr));

#define rep(i,n) for(int i=0;i<n;i++)
#define rep_1(i,n) for(int i=1;i<n;i++)
#define FOR(i,m,n,k) for(int i=m;i<n;i=i+k)

#define VI vector<int>
#define VVI vector< VI >
#define VIit VI::iterator
#define pi pair<int,int>
#define ui64 unsigned long long int
#define i64 long long int
#define pb(a) push_back(a)
#define mp make_pair


#define SI(a) scanf("%d",&a)
#define Si(a) scanf("%d",&a)
#define SU(a) scanf("%u",&a)
#define SHD(a) scanf("%hd",&a)
#define SHU(a) scanf("%hu",&a)
#define SLLD(a) scanf("%lld",&a)
#define SLLU(a) scanf("%llu",&a)
#define SF(a) scanf("%f",&a)
#define SLF(a) scanf("%lf",&a)
#define SC(a) scanf("%c",&a)
#define SS(a) scanf("%s",a)
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define PI acos(-1.0)
#define eps 1e-9

//bool CHECK(int num,int pos){ return num&(1<<(pos)); }
//int SET(int &num,int pos){ return num=(1<<(pos));}
//int CLEAR(int &num,int pos) {return num&=~(1<<(pos));}
//int TOGGLE(int num,int pos) {return num^=(1<<(pos)); }

#define fi first
#define se second
#define bg begin()
#define en end()
#define ALL(x) (x).begin(), (x).end()
#define TR(i,x) for(typeof(x.begin()) i=x.begin();i!=x.end();++i)
#define SZ(x) (int)(x.size())

bool comp (pair<pair <string , int >,int> a , pair<pair <string , int >,int> b)
{

    return a.first.first < b.first.first ;

}

int main()
{
    string s ;
    set<string> St ;
    while( cin >> s && s!="::") {
        St.insert( s ) ;
    }
    s.clear() ;
    string V[210] ;
    vector< pair<pair <string , int >,int> > Vp ;



    getchar() ;
    int index = 0 ;
    while( getline( cin , s ) ) {

        string temp = "" ;
        for( int i = 0 ; s[i] ; i++ ) {
            if( s[i] >='A' && s[i] <= 'Z' ) {
                s[i] = s[i] - 'A' + 'a' ;
            }
        }

        int i = 0 ;
        int lineindex = 1 ;
        while( s[i] ) {
            if( s[i] == ' ' ) {
                string tem = temp ;
                if( St.find(tem) == St.end() ) {
                    Vp.pb( mp(mp(tem, lineindex ) ,index)) ;
                }
                temp.clear();
                lineindex++;
            } else {
                temp += s[i] ;
            }
            i++;
        }


        string tem = temp ;
        if( St.find(tem) == St.end() ) {
            Vp.pb( mp(mp(tem, lineindex ) ,index)) ;
        }
        temp.clear();
         V[index]=  s  ;


        index++ ;
    }


    sort( Vp.begin() , Vp.end() , comp ) ;


    for( vector< pair<pair <string , int >,int> >::iterator it = Vp.begin() ; it!=Vp.end() ; it++ ) {

        int in = it->second ;
        pair <string , int > a = it->first ;
        string com = a.first ;
        int numword = a.second ;


        string printstr = V[in];

        int m = 1 ;
        if ( numword == 1 ) {
            for( int i = 0 ; printstr[i] && printstr[i]!=' ' ; i++) {
                if( printstr[i] >= 'a' && printstr[i] <= 'z') {
                    printstr[i] = printstr[i]-'a'+'A' ;
                }
            }
             cout << printstr << endl ;
        } else {
            for( int i = 0 ; printstr[i] ; i++ ) {

                if( printstr[i] == ' ' && printstr[i+1] != ' ' ) {
                    m++ ;
                    if( m == numword ) {
                        for( int j = i+1 ; printstr[j] && printstr[j]!=' ' ; j++ ) {
                            if( printstr[j] >= 'a' && printstr[j] <= 'z') {
                                printstr[j] = printstr[j]-'a'+'A' ;
                            }
                        }
                    }
                }

            }
             cout << printstr << endl ;
        }
    }
return 0 ;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 123 - 123 - Searching Quickly

Post by brianfry713 » Tue May 20, 2014 10:07 pm

For input:

Code: Select all

is
the
of
and
as
a
but
::
  Descent of Man
The    Ascent of Man
The Old   Man and The Sea
A Portrait of   The Artist As a Young Man
A  Man is a Man but Bubblesort IS A DOG
Output should be the same as the sample output.
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 1 (100-199)”