C++ 检查特定txt文件中的项目是否符合c++;-请说出那个号码USACO

C++ 检查特定txt文件中的项目是否符合c++;-请说出那个号码USACO,c++,algorithm,c++14,C++,Algorithm,C++14,我在解决这个问题时有些疑问——说出那个号码。 事情是这样的- 在威斯康星州的大型牧场主中,通常会给奶牛打上序列号,以取悦会计部门。不过,牛郎们并不欣赏这种归档系统的优势,他们希望用一个讨人喜欢的名字来称呼牛群成员,而不是说:“来吧,好好相处。” 通过编写一个程序,将奶牛的品牌序列号翻译成与该序列号唯一相关的可能名称,从而帮助可怜的牛仔。由于牛仔们现在都有手机,所以使用标准的按键(R)电话键盘映射从数字到字母(除了“Q”和“Z”): 可接受的牛名在名为“dict.txt”的文件中提供给您,其中包含

我在解决这个问题时有些疑问——说出那个号码。 事情是这样的-

在威斯康星州的大型牧场主中,通常会给奶牛打上序列号,以取悦会计部门。不过,牛郎们并不欣赏这种归档系统的优势,他们希望用一个讨人喜欢的名字来称呼牛群成员,而不是说:“来吧,好好相处。”

通过编写一个程序,将奶牛的品牌序列号翻译成与该序列号唯一相关的可能名称,从而帮助可怜的牛仔。由于牛仔们现在都有手机,所以使用标准的按键(R)电话键盘映射从数字到字母(除了“Q”和“Z”):

可接受的牛名在名为“dict.txt”的文件中提供给您,其中包含少于5000个可接受牛名(所有字母大写)的列表。取一头牛的品牌号,并报告该编号对应的所有可能单词中的哪些在给定词典中,该词典在分级环境中以dict.txt的形式提供(并按升序排序)

例如,品牌号4734产生以下所有名称:

GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI GRG GRH GRI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEI GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFI HRDG HRDH HRDI HRG HRH HRI HRFG HRFH HRFI HSDG HSDH HSDI HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEI IPEI IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFI ISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI 碰巧的是,这81个名字中唯一一个在有效名字列表中是“格雷格”

编写一个给定奶牛品牌号的程序,并打印该品牌号生成的所有有效名称,如果没有有效名称,则打印“无”。序列号可以长达十几位

以下是我试图解决这个问题的方法。只需检查列表中的所有名称,并检查哪些名称满足给定的约束条件

int numForChar(char c){
    if     (c=='A'||c=='B'||c=='C') return 2;
    else if(c=='D'||c=='E'||c=='F') return 3;
    else if(c=='G'||c=='H'||c=='I') return 4;
    else if(c=='J'||c=='K'||c=='L') return 5;
    else if(c=='M'||c=='N'||c=='O') return 6;
    else if(c=='P'||c=='R'||c=='S') return 7;
    else if(c=='T'||c=='U'||c=='V') return 8;
    else if(c=='W'||c=='X'||c=='Y') return 9;
    else return 0;

int main(){

    ios::sync_with_stdio(0);
    cin.tie(0);

    freopen("namenum.in","r",stdin);
    freopen("namenum.out","w",stdout);

    string S; cin >> S;
    int len = S.length();

    freopen("dict.txt","r",stdin);

    string x;

    while(cin >> x){
        string currName = x;
        if(currName.length() != S.length()) continue;
        string newString = x;
        for(int i=0;i<len;i++){
            //now encode the name as a number according to the rules
            int num = numForChar(currName[i]);
            currName[i] = (char)num;
        }
        if(currName == S){
            cout << newString << "\n";
        }
    }



    return 0;
}
int numForChar(字符c){
if(c=='A'| c=='B'| c=='c')返回2;
else if(c=='D'| c=='E'| c=='F')返回3;
else如果(c='G'| c=='H'| c=='I')返回4;
else if(c=='J'| c=='K'| c=='L')返回5;
else if(c=='M'| c=='N'| c=='O')返回6;
else if(c=='P'| c=='R'| c=='S')返回7;
else if(c='T'| c='U'| c='V')返回8;
else if(c=='W'| c=='X'| c=='Y')返回9;
否则返回0;
int main(){
ios::将_与_stdio同步(0);
cin.tie(0);
freopen(“namenum.in”,“r”,stdin);
freopen(“namenum.out”,“w”,stdout);
字符串S;cin>>S;
int len=S.length();
freopen(“dict.txt”、“r”、stdin);
字符串x;
而(cin>>x){
字符串currName=x;
如果(currName.length()!=S.length())继续;
字符串newString=x;

对于(int i=0;i我建议您使用C++文件处理。重写STDIN和STDUT似乎不合适。 加上这些,

std::ifstream dict ("dict.txt");
std::ofstream fout ("namenum.out");
std::ifstream fin ("namenum.in");
因此,

cin >> S  --to-->  fin >> S;
cin >> x  --to-->  dict >> x
cout << newString  --to--> fout << newString

cin>>S——到-->fin>>S;
cin>>x——到-->dict>>x

因此,在进一步研究了这个问题并探索了这个号码的信息之后,我意识到这不是一个当前的比赛,只是一个练习挑战。因此,我更新了我的答案,并给了你一个成功提交的版本。尽管如此,这是一个扰流器,将在为什么你的代码不工作之后发布国王

首先,您在声明number函数后忘记了}。其次,您没有实现任何检查输入是否无法生成有效名称的功能。第三,当您使用numForChar()时在currName字符上,函数生成了一个整数值。这不是问题,问题是它不是ASCII码,而是一个原始数字。然后将其与输入字符串的一个字符进行比较。其中,是一个数字的ASCII值。因此,您的代码永远找不到匹配项。要修复此问题,您只需在retu中添加48即可numForChar()函数的rn值或numForChar()的xor值返回48

您使用的方法是正确的。但是有一些提示。如果您感到厌烦,可以随时跳到扰流板。您不需要使用numForChar()函数从字符中实际获取数字值。您可以使用常量数组。常量数组比许多If循环快

例如,你知道A,B,C将产生2,A的ASCII码是65,B是66,C等于67。对于这3,你可以有一个由3个索引组成的数组,0,1,2,所有这些索引都存储了A2。因此,如果你得到B,减去B的ASCII码65将产生1。这就是从中获取值的索引

要获取字符的数字,可以使用字符的矩阵数组。跳过前2个索引0和1。每个一级索引包含3个字符的3个数组,这些数组适合它们的位置

对于字典比较,如果长度不相等,我们不需要实际查看单词是正确的。但是,除此之外,由于他们的字典单词是经过排序的,如果单词的第一个字母低于输入的第一个字母的范围,我们可以跳过它。另一方面,如果单词的第一个字母现在高于最高的字母对于输入的第一个字母,继续搜索是没有意义的。请注意,我对代码注释的英语几乎总是不好的,除非我广泛地记录它

您的代码(已修复):

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int numForChar(char c){
    if     (c=='A'||c=='B'||c=='C') return 2;
    else if(c=='D'||c=='E'||c=='F') return 3;
    else if(c=='G'||c=='H'||c=='I') return 4;
    else if(c=='J'||c=='K'||c=='L') return 5;
    else if(c=='M'||c=='N'||c=='O') return 6;
    else if(c=='P'||c=='R'||c=='S') return 7;
    else if(c=='T'||c=='U'||c=='V') return 8;
    else if(c=='W'||c=='X'||c=='Y') return 9;
    else return 0;
}

int main(){

    ios::sync_with_stdio(0);
    cin.tie(0);

    ifstream fin("namenum.in");
    ifstream dict("dict.txt");
    ofstream fout("namenum.out");

    string S; 
    fin >> S;
    int len = S.length();
    bool match = false;

    string x;

    while(dict >> x){
        string currName = x;
        if(currName.length() != S.length()) continue;
        string newString = x;
        for(int i=0;i<len;i++){
            //now encode the name as a number according to the rules
            int num = numForChar(currName[i]) ^ 48;
            currName[i] = (char)num;
        }
        if(currName == S){
            fout << newString << "\n";
            match = true;
        }
    }

    if ( match == false ){
        fout << "NONE" << endl;
    }

    return 0;
}
#include <fstream>
#include <string>

using namespace std;

// A = 65
// 65 - 0 = 65
const char wToN[] = {
//  A ,B  ,C  ,D  ,E  ,F  ,G  ,H  ,I  ,
   '2','2','2','3','3','3','4','4','4',
// J  ,K  ,L  ,M  ,N  ,O  ,P  ,Q  ,R  ,S
   '5','5','5','6','6','6','7','7','7','7',
//  T  ,U  ,V  ,W  ,X  ,Y  ,Z
    '8','8','8','9','9','9','9'     
};
// 2 = {A, B, C} = 2[0] = A, 2[1] = B, 2[2] C
const char nToW[10][3] = {
    {}, // 0 skip
    {}, // 1
    {'A','B','C'},
    {'D','E','F'},
    {'G','H','I'},
    {'J','K','L'},
    {'M','N','O'},
    {'P','R','S'},
    {'T','U','V'},
    {'W','X','Y'}
};

int main(){

    ifstream fin("namenum.in");
    ifstream dict("dict.txt");
    ofstream fout("namenum.out");

    string S; 
    fin >> S;

    // Since this will not change
    // make this a const to make it 
    // run faster.
    const int len = S.length();
    // lastlen is last Index of length
    // We calculate this value here,
    // So we do not have to calculate 
    // it for every loop.
    const int lastLen = len - 1;
    int i = 0;
    unsigned char digits[len];
    unsigned char firstLetter[3];
    // If not match print None
    bool match = false;

    for (  ; i < len; i++ ){
      // No need to check upper bound
      // constrain did not call for check.
      if ( S[i] < '2' ) {
        fout << "NONE" << endl;
        return 0;
      }
    }

    const char digit1 = S[0] ^ 48;
    // There are 3 set of first letter.
    // We get them by converting digits[0]'s
    // value using the nToW array.
    firstLetter[0] = nToW[digit1][0];
    firstLetter[1] = nToW[digit1][1];
    firstLetter[2] = nToW[digit1][2];

    string dictStr;

    while(dict >> dictStr){
      // For some reason, when keeping the i = 0 here
      // it seem to work faster. That could be because of compiler xor.
      i = 0;
      // If it is higher than our range 
      // then there is no point contineuing.
      if ( dictStr[0] > firstLetter[2] ) break;

      // Skip if first character is lower
      // than our range. or If they are not equal in length
      if ( dictStr[0] < firstLetter[0] || dictStr.length() != len ) continue;

      // If we are in the letter range
      // we always check the second letter
      // not the first, since we skip the first
      i = 1;
      for ( int j = 1; j < len; j++ ){
        // We convert each letter in the word
        // to the corresponding int value
        // by subtracting the word ASCII value 
        // to 65 and use it again our wToN array.
        // if it does not match the digits at 
        // this current position we end the loop.
        if ( wToN[dictStr[i] - 65] != S[j] ) break;

        // if we get here and there isn't an unmatch then it is a match.
        if ( j == lastLen ) {
            match = true;
            fout << dictStr << endl;
            break;
        }
        i++;
      }
    }
    // No match print none.

    if ( match == false ){
        fout << "NONE" << endl;
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int numForChar(字符c){
if(c=='A'| c=='B'| c=='c')返回2;
else if(c=='D'| c=='E'| c=='F')返回3;
else如果(c='G'| c=='H'| c=='I')返回4;
else if(c=='J'| c=='K'| c=='L')返回5;
else if(c=='M'| c=='N'| c=='O')返回6;
else if(c=='P'| c=='R'| c=='S')返回7;
埃尔斯
#include <fstream>
#include <string>

using namespace std;

// A = 65
// 65 - 0 = 65
const char wToN[] = {
//  A ,B  ,C  ,D  ,E  ,F  ,G  ,H  ,I  ,
   '2','2','2','3','3','3','4','4','4',
// J  ,K  ,L  ,M  ,N  ,O  ,P  ,Q  ,R  ,S
   '5','5','5','6','6','6','7','7','7','7',
//  T  ,U  ,V  ,W  ,X  ,Y  ,Z
    '8','8','8','9','9','9','9'     
};
// 2 = {A, B, C} = 2[0] = A, 2[1] = B, 2[2] C
const char nToW[10][3] = {
    {}, // 0 skip
    {}, // 1
    {'A','B','C'},
    {'D','E','F'},
    {'G','H','I'},
    {'J','K','L'},
    {'M','N','O'},
    {'P','R','S'},
    {'T','U','V'},
    {'W','X','Y'}
};

int main(){

    ifstream fin("namenum.in");
    ifstream dict("dict.txt");
    ofstream fout("namenum.out");

    string S; 
    fin >> S;

    // Since this will not change
    // make this a const to make it 
    // run faster.
    const int len = S.length();
    // lastlen is last Index of length
    // We calculate this value here,
    // So we do not have to calculate 
    // it for every loop.
    const int lastLen = len - 1;
    int i = 0;
    unsigned char digits[len];
    unsigned char firstLetter[3];
    // If not match print None
    bool match = false;

    for (  ; i < len; i++ ){
      // No need to check upper bound
      // constrain did not call for check.
      if ( S[i] < '2' ) {
        fout << "NONE" << endl;
        return 0;
      }
    }

    const char digit1 = S[0] ^ 48;
    // There are 3 set of first letter.
    // We get them by converting digits[0]'s
    // value using the nToW array.
    firstLetter[0] = nToW[digit1][0];
    firstLetter[1] = nToW[digit1][1];
    firstLetter[2] = nToW[digit1][2];

    string dictStr;

    while(dict >> dictStr){
      // For some reason, when keeping the i = 0 here
      // it seem to work faster. That could be because of compiler xor.
      i = 0;
      // If it is higher than our range 
      // then there is no point contineuing.
      if ( dictStr[0] > firstLetter[2] ) break;

      // Skip if first character is lower
      // than our range. or If they are not equal in length
      if ( dictStr[0] < firstLetter[0] || dictStr.length() != len ) continue;

      // If we are in the letter range
      // we always check the second letter
      // not the first, since we skip the first
      i = 1;
      for ( int j = 1; j < len; j++ ){
        // We convert each letter in the word
        // to the corresponding int value
        // by subtracting the word ASCII value 
        // to 65 and use it again our wToN array.
        // if it does not match the digits at 
        // this current position we end the loop.
        if ( wToN[dictStr[i] - 65] != S[j] ) break;

        // if we get here and there isn't an unmatch then it is a match.
        if ( j == lastLen ) {
            match = true;
            fout << dictStr << endl;
            break;
        }
        i++;
      }
    }
    // No match print none.

    if ( match == false ){
        fout << "NONE" << endl;
    }

    return 0;
}