hihoCoder1341

来源:互联网 发布:股票实时数据api 编辑:程序博客网 时间:2024/06/03 19:14

题目链接:hihoCoder1341


题目大意:现在给你n个约束,然后给你q种询问,每组询问分别对约束中的所有字母(A~Z)赋值,问该组询问的赋值是否满足所有的约束。


题目分析:字符串模拟题。刷的题还是太少了,写的时间太久了。


代码:

#include<cstdio>#include<string>#include<string.h>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int maxn=30;char ch;int n,q,cnt;bool vis[200];int value[200];string line[maxn];bool isop(char ch){    if(ch=='<'||ch=='>'||ch=='=')   return true;    return false;}bool cmp(int a,string op,int b){    if(op=="<"&&a<b)        return true;    else if(op==">"&&a>=b)  return true;    else if(op=="<="&&a<=b) return true;    else if(op==">="&&a>=b) return true;    return false;}void solve(int& a,int& b,int& val){    if(a!=-1) {        if(b!=-1){            a=b;        }        b=val;    } else a=val;    val=-1;}bool judge(){    for(int i=1;i<=n;i++){        string op="",pre="",str="";        int a=-1,b=-1,val=-1,len=line[i].length();        for(int j=0;j<len;j++){            if(isalpha(line[i][j])){                val=value[line[i][j]];            } else if (isdigit(line[i][j])){                str+=line[i][j];                while(isdigit(line[i][j+1])){                    str+=line[i][++j];                }                val=atoi(str.c_str());str="";            } else if (isop(line[i][j])){                op+=line[i][j];                while(isop(line[i][j+1])){                    op+=line[i][++j];                }                solve(a,b,val);                if(a!=-1&&b!=-1){                    if(!cmp(a,pre,b)){                        return false;                    }                }                pre=op;op="";            }        }    }    return true;}int main(){//    freopen("in.txt","r",stdin);    ios::sync_with_stdio(false);    cin>>n;    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++){        cin>>line[i];        int len=line[i].length();        for(int j=0;j<len;j++){            if(isop(line[i][j]))   continue;            if(!vis[line[i][j]]&&isalpha(line[i][j])){                cnt++;                vis[line[i][j]]=1;            }        }        line[i]+="=";    }    cin>>q;    for(int i=1;i<=q;i++){        memset(value,0,sizeof(value));        for(int j=1;j<=cnt;j++){            cin>>ch;            cin>>value[ch];        }        if(judge()){            cout<<"Yes"<<endl;        } else {            cout<<"No"<<endl;        }    }    return 0;}


原创粉丝点击