Code: Select all
#include<iostream>
#include<cstring>
#include <map>
#include<string>
#include<cmath>
#include<cctype>
#include <cstdlib>
using namespace std;
const int LineMax = 72;
const int NULLCHAR = '\0';
typedef char LineType[LineMax];
class LineClass
{
private:
LineType Info;
public:
LineClass(void);
LineClass(LineType Str);
void GetLine(LineType Line) const;
};
class frequen
{
private:
int cnt;
public:
frequen(int a)
{
cnt = a;
}
void inc()
{
cnt++;
}
int get()
{
return cnt;
}
};
LineClass::LineClass(void)
{
Info[0] = NULLCHAR;
}
LineClass::LineClass(LineType Str)
{
strcpy(Info, Str);
}
void LineClass::GetLine(LineType Line) const
{
strcpy(Line, Info);
}
bool operator<(LineClass LHS, LineClass RHS)
{
LineType Left, Right;
LHS.GetLine(Left);
RHS.GetLine(Right);
if (strcmp(Left, Right) < 0)
return true;
else
return false;
}
void LoadData(map<LineClass,frequen> & Dictionary);
void Lookup(LineType Target, map<LineClass, frequen> & Dictionary);
class BigInteger
{
public:
BigInteger();
int setbychar(const char *num);
int add(BigInteger &a1,BigInteger &a2);
const char*print();
~BigInteger();
private:
int size;
char *content;
};
BigInteger :: BigInteger() : size(0),content(0){}
int BigInteger::setbychar(const char *num)
{
size = strlen(num);
content = new char[size+1];
strcpy(content,num);
return 0;
}
int BigInteger::add(BigInteger &a1, BigInteger &a2)
{
char *temp;
int i,j,k,c,t;
size = a1.size > a2.size ? a1.size: a2.size;
size++;
temp = new char [size+1];
memset(temp,32,sizeof(temp));
i = a1.size -1;
j = a2.size - 1;
k = size - 1;
temp[size] = '\0';
c = 0;
while( i>= 0 || j >= 0 || c != 0)
{
t = c;
c = 0;
if( i >= 0)
{
t += a1.content[i] - 48;
i--;
}
if( j >= 0 )
{
t += a2.content[j] - 48;
j--;
}
c = t/10;
t = t%10;
temp[k--] = t + 48;
}
if( k >= 0)
{
strcpy(temp,temp+k+1);
}
size -= (k+1);
if( content != NULL)
delete content;
content = temp;
return 0;
}
const char *BigInteger::print()
{
return content;
}
BigInteger::~BigInteger()
{
if( content != NULL)
{
delete content;
}
}
//if(myMap.find("STL") != myMap.end()) { ... }
int main(void)
{
int i,n;
char in[10000];
int value;
char sum[1000000];
char temp[1000000];
map<LineClass, frequen> Dictionary;
LineType Target;
map<LineClass,frequen>::iterator it;
pair<map<LineClass, frequen>::iterator,bool> ret;
int a,b;
BigInteger a1[5001];
cin >> a >> b;
getchar();
for( i = 0; i < a ; i++)
{
cin >> in >> value;
getchar();
ret = Dictionary.insert(pair<LineClass,frequen>(LineClass(in),
frequen(value)));
}
getchar();
char *p;
char val[100000];
while(b--)
{
sum[0] = '\0';
while(1)
{
gets(in);
if(strlen(in) == 1 && in[0] == '.')
break;
p = strtok(in," ,.,!?&@#\"");
while( p != NULL)
{
if( Dictionary.find(LineClass(p)) != Dictionary.end())
{
int num = Dictionary.find(LineClass(p)) -> second.get();
//itoa (num,val,10);
sprintf(val, "%d", num);
a1[0].setbychar(val);
a1[1].setbychar(sum);
a1[2].add(a1[0],a1[1]);
strcpy(sum,a1[2].print());
}
p = strtok(NULL," ,.,!?&@#\"");
}
}
cout << sum << endl;
}
return 0;
}