Problem with map initialization

Revision en1, by jakeob, 2024-11-13 00:32:23

The below code is the solution to some USACO problem and I when submitting I noticed that my submission did not get the full credit so I spend some time looking around my code and found out that there is somehow a problem with the initialization of the map when I initialize map m like this the: '''c++ string s[]={"Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog"," Pig", "Rat"}; map<string, int> m; for(int i=0;i<12;i++){ m[s[i]]=i; } ''' the code doesn't pass all test cases. But when I did it manually like in the example below it works perfectly. So I would like to ask you why that is? '''c++

include <bits/stdc++.h>

int main(){ string s[]={"Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog"," Pig", "Rat"}; map<string, int> m; /*for(int i=0;i<12;i++){ m[s[i]]=i; }*/ m["Ox"] = 0; m["Tiger"] = 1; m["Rabbit"] = 2; m["Dragon"] = 3; m["Snake"] = 4; m["Horse"] = 5; m["Goat"] = 6; m["Monkey"] = 7; m["Rooster"] = 8; m["Dog"] = 9; m["Pig"] = 10; m["Rat"] = 11; int n;cin>>n; map<string,int>ma; map<string,string>yr; yr["Bessie"]="Ox"; ma["Bessie"]=0; for(int i=0;i<n;i++){ string cow1,cow2,when,how; cin>>cow1; for(int j=0;j<2;j++){ string h;cin>>h; }

cin>>how;
  cin>>when;
  for(int j=0;j<2;j++){
    string g;cin>>g;
  }
  cin>>cow2;
  int dif=0;
  if(how=="previous"){
      if((m[when]-m[yr[cow2]])==0){
        dif=12;
      }
      else if(m[when]>m[yr[cow2]]){
        dif=12-m[when]+m[yr[cow2]];
      }
      else{
        dif=m[yr[cow2]]-m[when];
      }
      ma[cow1]=ma[cow2]-dif;
  }
  else{

      if((m[when]-m[yr[cow2]])==0){
        dif=12;
      }
      else if(m[when]>m[yr[cow2]]){
        dif=m[when]-m[yr[cow2]];
      }
      else{
        dif=12+m[when]-m[yr[cow2]];
      }
      ma[cow1]=ma[cow2]+dif;          
  }
  yr[cow1]=when;
}

cout<<abs(ma["Elsie"])<<"\n";

} '''

Tags maps

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English jakeob 2024-11-13 00:34:52 89
en1 English jakeob 2024-11-13 00:32:23 2259 Initial revision (published)