codeforces 402 E

TLE好多次,原来是map的原因,用map做一个数组使用的话,速度会慢上许多哟!!

#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include<algorithm>
#include <string.h>
#include <map>
using namespace std;
const long long INF=1e15+7;
struct ttt{
int a;
int b;
string z;
int suan;
};
//map<int,ttt>qq;
ttt qq[5050];
map<string,int>q1;
int walk[5050];
int walked[2][5050][2];
int k;
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int i,j,l1,l2,l3,f1,f2,f3,t1,t2,t3;
    string e;
    string c;
    int n,m;
    cin >> n>> m;
    q1["?"]=0;
    for(i=1;i<=n;i++){
    cin >> e;       //      //string需要用cin 输入
    q1[e]=i;
    scanf(" := " );
    cin >> c;
    if(c[0]=='0'||c[0]=='1'){
        qq[i].z=c;
    // cout << "i=" <<i <<endl;
        //strcpy(qq[d].z,c);
    }else{
        qq[i].a=q1[c];
    //strcpy(qq[d].a,c);
    //scanf("%s",c);
    cin >> c;
    if(c[0]=='A')qq[i].suan=0;
    if(c[0]=='O')qq[i].suan=1;
    if(c[0]=='X')qq[i].suan=2;
    //scanf("%s",c);
    cin >> c;
    qq[i].b=q1[c];
    //strcpy(qq[d].b,c);
    //cout << c<< endl;
    }
    //gets(c);
    }
    memset(walked,0,sizeof(walked));
    int c1,c2;
    for(i=0;i<m;i++){
            //cout << "i=" << i << endl;
            //walk.clear();
       for(k=0;k<=1;k++)
       for(j=1;j<=n;j++){
            if(qq[j].z[0]=='0'||qq[j].z[0]=='1'){
                //cout << k <<endl;
                walk[j]=qq[j].z[i]-'0';  //-'0'的区别与-0
                c1=qq[j].z[i]-'0';
              //  cout <<"C1=" <<c1 <<endl;
                }else{
                if(qq[j].a==0)
                    c1=k;
                else
                c1=walk[qq[j].a];

                if(qq[j].b==0)
                    c2=k;
                else
                c2=walk[qq[j].b];
        if(qq[j].suan==0){
            c1=c1&c2;
        }else if(qq[j].suan==1){
            c1=c1|c2;
        }else if(qq[j].suan==2){
            c1=c1^c2;
        }
        walk[j]=c1;
         }
       //if(i==2){
        //cout << "k=" <<k << "i=" << i<< " j=" << j << "c1= "<< c1 << endl;
      // }
      // cout <<c1 <<endl;
         walked[k][i][c1]++;
       }
    }
    int sum1=0;
    int sum2=0;
    /*for(i=0;i<m;i++){
            sum1+=pow(2,m-1-i)*max(walked[0][i][1],walked[1][i][1]);
            sum2+=pow(2,m-1-i)*(n-max(walked[0][i][0],walked[1][i][0]));
        }
        cout << sum1<<endl;
        cout << sum2 << endl;*/
    string cc1,cc2;
    cc1="";
    cc2="";
    for(i=0;i<m;i++){
        if(walked[0][i][0]>=walked[1][i][0])
            cc1+='0';
        else
            cc1+='1';
            if(walked[0][i][1]>=walked[1][i][1])
            cc2+='0';
        else
            cc2+='1';
    }
    cout << cc1 <<endl;
    cout << cc2 <<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值