华电北风吹
天津大学认知计算与应用重点实验室
2016-07-17
题目链接:
http://hihocoder.com/problemset/problem/1341
题目分析:
题目比较简单,一个一个匹配即可。
参考代码:
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <sstream>
using namespace std;
int data[26];
int CountLetter(vector<string> & constrain)
{
bool hasLetter[26];
memset(hasLetter, false, sizeof(hasLetter));
for (int i = 0; i < constrain.size(); i++)
{
for (int j = 0; j < constrain[i].size(); j++)
{
int id = constrain[i][j] - 'A';
if ((id >= 0) && (id < 26))
{
hasLetter[id] = true;
}
}
}
int count = 0;
for (int i = 0; i < 26; i++)
{
count += hasLetter[i] ? 1 : 0;
}
return count;
}
void split(string &s, string &delim, vector<string>& ret)
{
size_t last = 0;
size_t index = s.find_first_of(delim, last);
while (index != string::npos)
{
ret.push_back(s.substr(last, index - last));
last = index + 1;
index = s.find_first_of(delim, last);
}
if (index - last>0)
{
ret.push_back(s.substr(last, index - last));
}
}
int GetValue(string s)
{
if (s[0] == '=')
{
s = s.substr(1);
}
if (s.size() > 1)
{
stringstream ss;
ss << s;
int m;
ss >> m;
return m;
}
else
{
int id = s[0] - 'A';
if (id>=0)
{
return data[id];
}
else
{
return s[0] - '0';
}
}
}
bool CheckString(string &s)
{
string delim = "<";
vector<string> ret;
split(s, delim, ret);
for (int i = 0; i < ret.size() - 1; i++)
{
bool equal = false;
if (ret[i + 1][0] == '=')
equal = true;
int v1 = GetValue(ret[i]);
int v2 = GetValue(ret[i + 1]);
if (v1 > v2)
{
return false;
}
if ((v1 == v2) && (equal == false))
{
return false;
}
}
return true;
}
bool Check(vector<string> & constrain)
{
for (int i = 0; i < constrain.size(); i++)
{
if (CheckString(constrain[i]) == false)
return false;
}
return true;
}
int main()
{
int N;
cin >> N;
vector<string> constrain(N);
for (int i = 0; i < N; i++)
cin >> constrain[i];
int letterCount = CountLetter(constrain);
int T;
cin >> T;
while (T--)
{
memset(data, 0, sizeof(data));
for (int i = 0; i < letterCount; i++)
{
char valuename;
int value;
cin >> valuename;
cin >> value;
data[valuename - 'A'] = value;
}
if (Check(constrain))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}