zoj 1038 T9 mobile input method

#include <cstdlib>

#include <iostream>

#include <cstring>



using namespace std;



struct Key{

       int num;

       char ch[4];

};



const Key keys[10] = {

   {}, {},             {3, {'a', 'b', 'c'}}, {3, {'d', 'e', 'f'}}, 

   {3, {'g','h','i'}}, {3, {'j', 'k', 'l'}},   {3, {'m','n','o'}}, 

   {4, {'p', 'q','r', 's'}},  {3, { 't', 'u', 'v'}}, {4,{'w', 'x','y','z'}}

};

void printKeyBoard()

{

   for (int i = 2; i < 10; i++)

   {

       cout<<"Key "<<i<<":";

       for (int j = 0; j < keys[i].num; j++)

         cout<<keys[i].ch[j];

       cout<<endl;

   }

}

struct MyWord{

       char s[101];

       int len;

       int freq;

       bool prefix;

};



MyWord word[1001];

int    tFreq[1001];

int nWords;



inline void readScenario()

{

     cin>>nWords;

     for (int i = 0; i < nWords; i++)

     {

         cin>>word[i].s>>word[i].freq;

         word[i].len = strlen(word[i].s);

     }

}



inline void markWords(char qry[], int n)

{

    for (int i = 0; i < nWords; i++)

    {

       if (!word[i].prefix ) continue;

       else if (word[i].len < n) { word[i].prefix = false; continue; }

       int k;

       for (k = 0; k < keys[qry[n-1]].num; k++)

            if (keys[qry[n-1]].ch[k] == word[i].s[n-1]) break;

       word[i].prefix  =  k<keys[qry[n-1]].num;

    }

}



inline void mostFreqStr(char qry[], int n, char* s)

{

     markWords(qry, n);

     for (int i = 0; i < nWords; i++)

     {

         tFreq[i] = 0;

         if ( !word[i].prefix ) continue;

         for (int j = 0; j < nWords; j++)

            if (word[j].prefix && 0 == strncmp(word[i].s, word[j].s, n))

              tFreq[i] += word[j].freq;

     }

     int idx = -1, max = -1;

     for(int i = 0; i < nWords; i++)

       if (word[i].prefix && max < tFreq[i]) { idx = i; max = tFreq[i]; }

     if (max == -1)

     {

             strcpy(s, "MANUALLY/0");

             return ;

     }

     strncpy(s, word[idx].s, n);

     s[n] = '/0';

     return ;

}



int main()

{

    //printKeyBoard();

    int t;// test cases

    int n; //Num of query strings

    char query[101]; char s[101];

    cin>>t;

    for (int k = 1; k <= t; k++)

    {

        cout<<"Scenario #"<<k<<":"<<endl;

        readScenario();

        cin>>n;

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

        {

            int j;

            for (j = 0; j < nWords; j++) word[j].prefix = true;

            j = 0; 

            while (cin>>query[j], query[j] -= '0', query[j++] != 1)

            {

                 mostFreqStr(query, j, s);

                 cout<<s<<endl;

            }

            cout<<endl;

        }

        cout<<endl;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值