-1

The problem statement:

Among the large Wisconsin cattle ranchers, it is customary to brand cows with serial numbers to please the Accounting Department. The cow hands don't appreciate the advantage of this filing system, though, and wish to call the members of their herd by a pleasing name rather than saying, "C'mon, #4734, get along."

Help the poor cowhands out by writing a program that will translate the brand serial number of a cow into possible names uniquely associated with that serial number. Since the cow hands all have cellular saddle phones these days, use the standard Touch-Tone(R) telephone keypad mapping to get from numbers to letters (except for "Q" and "Z"):

              2: A,B,C     5: J,K,L    8: T,U,V
              3: D,E,F     6: M,N,O    9: W,X,Y
              4: G,H,I     7: P,R,S

Acceptable names for cattle are provided to you in a file named "dict.txt", which contains a list of fewer than 5,000 acceptable cattle names (all letters capitalized). Take a cow's brand number and report which of all the possible words to which that number maps are in the given dictionary which is supplied as dict.txt in the grading environment (and is sorted into ascending order).

For instance, the brand number 4734 produces all the following names:

    GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI
    GREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEI
    GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFI
    HRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDI
    HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEI
    IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFI
    ISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI
    As it happens, the only one of these 81 names that is in the list of valid names is "GREG".
    

Write a program that is given the brand number of a cow and prints all the valid names that can be generated from that brand number or ``NONE'' if there are no valid names. Serial numbers can be as many as a dozen digits long.

  PROGRAM NAME: namenum
  INPUT FORMAT
      A single line with a number from 1 through 12 digits in length.
      SAMPLE INPUT (file namenum.in)
        4734
      OUTPUT FORMAT
        A list of valid names that can be generated from the input, one per line, in ascending alphabetical order.
      SAMPLE OUTPUT (file namenum.out)
        GREG

My code:

#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    ofstream fout("namenum.out");
    ifstream fin("namenum.in");
    long long int n,n1;
    fin>>n;
    int d=0; n1=n;
    while(n1){
        n1/=10;
        d++;
    }
    n1=n;
    unordered_map<int,vector<char>> m={{2,{'A','B','C'}},
                                   {3,{'D','E','F'}},
                                   {4,{'G','H','I'}},
                                   {5,{'J','K','L'}},
                                   {6,{'M','N','O'}},
                                   {7,{'P','R','S'}},
                                   {8,{'T','U','V'}},
                                   {9,{'W','X','Y'}}};
    fin.open("dict.txt");
    string s;
    fin>>s;
    vector<string> ans;
    while(!fin.eof()){
        
        if(s.length()==d){
            int flag=1,check=0;
            for(int i=d-1;i>=0;i--){
                flag=1;
                for(int j=0;j<3;j++){
                    if(m[n1%10][j]==s[i]){
                        flag=0;
                    }
                }
                n1/=10;
                if(flag==1){
                    check=1;
                    break;
                }
            }
            if(check==0){
                ans.push_back(s);
            }
            n1=n;
        }
    }
    sort(ans.begin(),ans.end());
    for(int i=0;i<ans.size();i++){
        fout<<ans[i]<<'\n';
    }
    return 0;
}

The following error is occuring:

 > Run 1: Execution error: Your program (`namenum') used more than
        the allotted runtime of 1 seconds (it ended or was stopped at
        1.855 seconds) when presented with test case 1. It used 1360 KB of
        memory. 

        ------ Data for Run 1 [length=5 bytes] ------
        4734 
        ----------------------------
    Test 1: RUNTIME 1.855>1 (1360 KB)

I don't get this error. How am i supposed to calculate the time and rectify my code accordingly?

itti_da
  • 65
  • 8
  • You're running this in an online service and that service caps your CPU use to one second, right? In that case, it's not a programming problem. You might get under that limit by somehow speeding up your program. I'd rather install a compiler locally and also a debugger so you can step through your code conveniently, though. – Ulrich Eckhardt May 31 '21 at 07:27
  • @UlrichEckhardt I disagree. It is not an online compiler time restriction. It mentions test cases. This means it is a typical TLE (Time Limit Exceeded) case of online judges for challenges and homeworks. The solution is of course still the same: Improve the efficiency of the algorithm. – Yunnosch May 31 '21 at 07:33
  • Aditi, the problem of TLE is not reduced by using those code-golfing habits. I recommend to drop them if your goal is to learn programming for a professional purpose. – Yunnosch May 31 '21 at 07:35
  • @Yunnosch what are code-golfing habits? I'm sorry i don't know a lot of things. About the efficiency, yah i was trying to come up with something better but i haven't till now :(( – itti_da May 31 '21 at 08:09
  • There's not a single read from `fin` in the while loop. No wonder the timeout of the testing platform was exceeded. Furthermore your program does not seem to be able to deal with a dictionary that only contains a single name... – fabian May 31 '21 at 08:10
  • @fabian thanks .. there was a mistake in reading input – itti_da May 31 '21 at 08:24
  • 1
    I didn't mean the compilation is timing out, @Yunnosch. :) Still, point is that the error is from something external to the program, like what some online learning platforms provide. Could also be a test framework run locally, but intuition tells me that is not the case here. – Ulrich Eckhardt May 31 '21 at 08:48
  • @UlrichEckhardt I agree. I seem to have mistaken your recommendation to compile, test and debug locally (which agree with) as a solution idea (which I meant to contradict only). – Yunnosch May 31 '21 at 08:56

1 Answers1

0

Done. Was not reading the input correctly. Just corrected the input of dict.txt and it's fine.

#include<bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    ofstream fout("namenum.out");
    ifstream fin("namenum.in");
    fstream file;
    long long int n,n1;
    fin>>n;
    int d=0; n1=n;
    while(n1){
        n1/=10;
        d++;
    }
    n1=n;
    unordered_map<int,vector<char>> m={{2,{'A','B','C'}},
                                   {3,{'D','E','F'}},
                                   {4,{'G','H','I'}},
                                   {5,{'J','K','L'}},
                                   {6,{'M','N','O'}},
                                   {7,{'P','R','S'}},
                                   {8,{'T','U','V'}},
                                   {9,{'W','X','Y'}}};
    file.open("dict.txt");
    string s;
    vector<string> ans;
    while(getline(file,s)){
        if(s.length()==d){
            int flag=1,check=0;
            for(int i=d-1;i>=0;i--){
                flag=1;
                for(int j=0;j<3;j++){
                    if(m[n1%10][j]==s[i]){
                        flag=0;
                    }
                }
                n1/=10;
                if(flag==1){
                    check=1;
                    break;
                }
            }
            if(check==0){
                ans.push_back(s);
            }
            n1=n;
        }
    }
    file.close();
    sort(ans.begin(),ans.end());
    for(int i=0;i<ans.size();i++){
        fout<<ans[i]<<'\n';
    }
    if(ans.size()==0){
        fout<<"NONE\n";
    }
    return 0;
}
itti_da
  • 65
  • 8
  • Things I don't like about this solution: including bits, using namespace std. Well done getting rid of `while(!fin.eof())` however. – Yunnosch May 31 '21 at 08:59
  • @Yunnosch yeah,I didn't use bits before but i started using it because it saves me some time as i don't need to mention different libraries and std:: every time in my program:))) And i am currently focusing on improving algos and problem solving skills so i am ignoring these details for the time being:))) – itti_da May 31 '21 at 09:16
  • If you aim to become a professional you are saving the wrong time and take a credit/loan on future time for maintenance, porting, debugging. Really, drop that habit again before it hardens.... Info so that you can decide yourself instead of just believing me (never do that... ;-) ) https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – Yunnosch May 31 '21 at 10:04
  • @Yunnosch. Oh okay, gotchya. :))tnkkks – itti_da May 31 '21 at 10:40