传送门

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Given a set of constraints like 0<N<=M<=100 and values for all the variables, write a checker program to determine if the constraints are satisfied.

More precisely, the format of constraints is:

token op token op ... op token

where each token is either a constant integer or a variable represented by a capital letter and each op is either less-than ( < ) or less-than-or-equal-to ( <= ).

输入

The first line contains an integer N, the number of constraints. (1 ≤ N ≤ 20)

Each of the following N lines contains a constraint in the previous mentioned format.

Then follows an integer T, the number of assignments to check. (1 ≤ T ≤ 50)

Each assignment occupies K lines where K is the number of variables in the constraints.

Each line contains a capital letter and an integer, representing a variable and its value.

It is guaranteed that:

1. Every token in the constraints is either an integer from 0 to 1000000 or an variable represented by a capital letter from 'A' to 'Z'.

2. There is no space in the constraints.

3. In each assignment every variable appears exactly once and its value is from 0 to 1000000.

输出

For each assignment output Yes or No indicating if the constraints are satisfied.

样例输入
2
A<B<=E
3<=E<5
2
A 1
B 2
E 3
A 3
B 5
E 10 样例输出
Yes
No

比较简单的字符串处理题。写这篇是要记录下C++的istream类的用法。

朴素实现:

#include <bits/stdc++.h>
using namespace std; map<char,int> mp; string c[];
bool used[]; bool ok(int a, string &op, int b){
if(op=="<") return a<b;
return a<=b;
} int get_val(string &s, int &i){
int res=;
for(; s[i] && isdigit(s[i]) && !isalpha(s[i]); res*=, res+=s[i++]-'');
if(isalpha(s[i])) res=mp[s[i++]];
return res;
} string get_op(string &s, int &i){
string res;
for(; s[i] && ispunct(s[i]); res+=s[i++]);
return res;
} bool check(int n){
int a, b;
string op;
for(int i=; i<n; i++){
int j=;
a=get_val(c[i], j);
for(;;){
op=get_op(c[i], j);
if(op=="") break;
b=get_val(c[i], j);
if(!ok(a, op, b)) return false;
a=b;
}
}
return true;
} int main(){
int n, T;
cin>>n;
for(int i=; i<n; i++){
cin>>c[i];
for(auto x:c[i])
if(isalpha(x)) used[x-'A']=true;
}
int nv=;
for(int i=; i<; i++) nv+=used[i]; for(cin>>T; T--; ){
for(int i=; i<nv; i++){
char x;
int v;
cin>>x>>v;
mp[x]=v;
}
puts(check(n)?"Yes":"No");
}
}
借助stringstream类的实现:
#include <bits/stdc++.h>
using namespace std; map<char,int> mp; string c[];
bool used[]; int get_val(stringstream &x){
int res;
char v;
x>>res;
if(x.fail()){ //check whether badbit or failbit is set
//When an istream object turns fail, it stops working until flags reset
x.clear(); //new
x.get(v), res=mp[v];
}
return res;
} string get_op(stringstream &x){
char v;
string res;
if(x.str().empty()) return res;
for(;;){
x.get(v);
if(ispunct(v)) res+=v;
else{
x.putback(v);
return res;
}
}
} bool ok(int a, string &op, int b){
if(op=="<") return a<b;
return a<=b;
} bool check(int n){
int a, b;
string op;
stringstream x;
for(int i=; i<n; i++){
x.str(c[i]);
x.clear(); //error-prone, new
a=get_val(x);
for(;;){
op=get_op(x);
if(op=="") break;
b=get_val(x);
if(!ok(a, op, b)) return false;
a=b;
}
}
return true;
} // Input stream objects can read and interpret input from sequences of characters.
// Specific members are provided to perform
// these input operations. The standard object cin is an object of this type. int main(){
int n, T;
cin>>n;
for(int i=; i<n; i++){
cin>>c[i];
for(auto x:c[i])
if(isalpha(x)) used[x-'A']=true;
}
int nv=;
for(int i=; i<; i++) nv+=used[i]; for(cin>>T; T--; ){
for(int i=; i<nv; i++){
char x;
int v;
cin>>x>>v;
mp[x]=v;
}
puts(check(n)?"Yes":"No");
}
}

这里系统介绍一下C++的Stream I/O. (以下内容来自 The C++ Programming Language 4th Ed. by Bjarne Stroustrup, 38 I/O Streams).

  • The I/O stream library provides formatted and unformatted buffered I/O of text and numeric values. The definitions for I/O stream facilities are found in <istream>, <ostream>, etc.;
  • An istream converts a stream of characters (bytes) to typed objects:

Byte sequences --> stream buffer --> istream --> typed values

An iostream is a stream that can act as both an istream and an ostream. You need stream buffers (streambufs) to define a mapping from an iostream to a new kind of device, file, or provide a new locale, you need a copy of the standard, a good systems manual, and examples of working code in addition to what is presented here.

The key components of the stream I/O system can be represented graphically like this:

    ios_base:           |-------------------------------------->|---------------------------------->locale: format information                   

locale independent format state        |              | 

    ^               |------> basic_streambuf<>: --------------------------------> real destination/source:

    I                |    buffering     |             

    I                |                | 

  basic_ios<>: ------------------------------------|             |--------------------------------> character buffer

locale dependent format state

  stream state

    ^

    |

    |

 basic_iostream<>:

formatting (<<, >>, etc.)

  setup/cleanup

The vertical arrows represent "derived from." The horizontal arrows represent "pointer to." The classes marked with <> are templates parameterized by a character type and containing a locale.

38.3 Error Handling

An iostream can be in one of four states, defined in basic_ios from <ios>:


Stream States


good()  The previous iostream operations succeeded

eof()     We hit end-of-input ("end-of-file")

fail()   Something unexpected hapened (e.g., we looked for a digit and found 'x')

bad()    Something unexpected and serious happened (e.g., disk read error)


Any operation attempted on a stream that is not in the good() state has no effect; it is a no-op. An iostream can be used as a condition. In that case, the condition is true (succeeds) if the state of the iostream is good().

38.4.4 Stream State

In <ios>, the standard library defines the base class ios_base defining most of the interface to a stream class.

The basic_ios class manages the state of a stream:

  • The mapping between a stream and its buffers
  • The formatting options
  • The use of locales
  • Error handling
  • Connections to other streams and stdio

It might be the most complicated class in the standard library.


ios_base Stream State iostate Member Constants


badbit  Something unexpected and serious happened (e.g., a disk read error)

failbit  Something unexpected hppened (e.g., we looked for a digit and found 'x')

eofbit  We hit end-of-input (e.g., end-of-file)

goodbit All is well


Functions for reading these bits (good(), fail(), etc.) in a stream are provided by basic_ios.

												

hihocoder #1341 Constraint Checker的更多相关文章

  1. hihocoder 1341 Constraint Checker【string】

    hihocoder 1341 解释:这道题题目还是比较容易理解,就是根据输入的若干个不等式,校验后面输入的数据是否都满足前面的不等式,满足就输出Yes,只要有一个不满足就输出No.如“A<B&l ...

  2. 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method

    异常:java.lang.LinkageError: loader constraint violation: when resolving interface method "javax. ...

  3. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  4. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

  5. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...

  6. 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II

    http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...

  7. 【hihocoder#1413】Rikka with String 后缀自动机 + 差分

    搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...

  8. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  9. 【hihoCoder】1288 : Font Size

    题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...

随机推荐

  1. hadoop家族之pig入门

    昨天成功运行第一个在hadoop集群上面的python版本的wordcount,今天白天继续看网上提供的文档.下午上头给定的回复是把hadoop家族都熟悉一下,那就恭敬不如从命,开始学习pig吧- 这 ...

  2. lecture9-提高模型泛化能力的方法

    HInton第9课,这节课没有放论文进去.....如有不对之处还望指正.话说hinton的课果然信息量够大.推荐认真看PRML<Pattern Recognition and Machine L ...

  3. opencv6.5-imgproc图像处理模块之轮廓

    接opencv6.4-imgproc图像处理模块之直方图与模板 这部分的<opencv_tutorial>上都是直接上代码,没有原理部分的解释的. 十一.轮廓 1.图像中找轮廓 /// 转 ...

  4. 支付宝Cookie高危漏洞引发的思考

    背景:当时我在做公司的网站支付接入,在调试支付宝WAP支付时,发现一些匪夷所思的事情: 1.我想要切换账号时退到需要输入登录信息时,原账号并没有退出,我按一下后退键又回来了: 2.我关闭浏览器也没有退 ...

  5. 汤姆大叔的6道javascript编程题题解

    看汤姆大叔的博文,其中有篇(猛戳这里)的最后有6道编程题,于是我也试试,大家都可以先试试. 1.找出数字数组中最大的元素(使用Math.max函数) var a = [1, 2, 3, 6, 5, 4 ...

  6. GridView的 OnRowDataBound 事件用法

    <asp:GridView ID="RptUsers" runat="server" AutoGenerateColumns="False&qu ...

  7. 几张图弄明白ios布局中的尺寸问题

    背景 先说说逆向那事.各种曲折..各种技术过时,老老实实在啃看雪的帖子..更新会有的. 回正题,这里讨论的是在Masnory框架下的布局问题.像我这种游击队没师傅带,什么都得自己琢磨,一直没闹明白下面 ...

  8. Sublime Text 3 常用插件以及安装方法(vue 插件)

    使用Package Control组件安装 也可以安装package control组件,然后直接在线安装: 按Ctrl+` 调出console 粘贴以下代码到底部命令行并回车: { import u ...

  9. 1008win7与虚拟机中的linux共享文件的(详细)方法

    转自http://jingyan.baidu.com/article/ca00d56c74dde4e99eebcfd2.html 好东西一起分享 win7与虚拟机中的linux共享文件的(详细)方法 ...

  10. 【CodeVS 2822】爱在心中

    “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度里有N个人,在他们的心中都有着一个 ...