#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;
}
}
zoj 1038 T9 mobile input method
最新推荐文章于 2017-03-27 21:02:00 发布