You are not logged in. Please login at www.codechef.com to post your questions!

×

whats wrong in my code

Question code: SHKSTR

My code:

include <bits stdc++.h="">

using namespace std;

typedef long long ll;

typedef long double ld;

struct str {

string s1;

int index;

};

int fun(str x,str y) {

if(x.s1.compare(y.s1)<0)
return 1;
else
return 0;

}

int main() {

int n;
cin>>n;
string a[n];
str s[n];
unordered_map<string,vector<int>> m;

for(int i=0;i<n;i++){

    cin>>s[i].s1;
    a[i]=s[i].s1;
    s[i].index=i;

}

sort(s,s+n,fun);

for(int i=0;i<n;i++){

    for(int j=0;j<s[i].s1.length();j++){

        m[s[i].s1.substr(0,j+1)].push_back(s[i].index);

    }

}

int q;

cin>>q;

while(q--)
{
    int r,f,ctr=0,t=0;

    cin>>r;

    string p;

    cin>>p;

    for(int i=p.length();i>=0;i--){

        f=1;

        ctr=0;

        if(m.find(p.substr(0,i)) != m.end()){

            for(int j=0;j<m[p.substr(0,i)].size();j++){

                if(m[p.substr(0,i)][j]<r){

                    f=0;

                    cout<<a[m[p.substr(0,i)][j]]<<endl;

                    break;

                }

            }

        }

        if(f==0)
        break;
    }

    if(f==1)
    cout<<a[0]<<endl;

}

}

asked 13 Jun, 18:02

srinivaskarri's gravatar image

4★srinivaskarri
32
accept rate: 0%

edited 13 Jun, 18:13

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15

question asked: 13 Jun, 18:02

question was seen: 39 times

last updated: 13 Jun, 18:13