1 条题解

  • 0
    @ 2021-6-15 1:39:00

    C++ :

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <sstream>
    #include <cstring>
    #include <stack>
    using namespace std;
    
    const int N = 27, MOD = 0x7fffffff;
    long long Std_Value[N], v[N];
    string s;
    istringstream ss;
    
    struct State
    {
    	long long v, c1, c2;
    	char c3;
    	
    	State() {}
    	State(long long V, long long C1, long long C2, char C3) {v = V; c1 = C1; c2 = C2; c3 = C3;}
    } C;
    stack<State> st[N];
    long long Pow(long long a, int b)
    {
    	long long c = 1;
    	while (b)
    	{
    		if (b & 1) c = c * a % MOD;
    		a = a * a % MOD;
    		b >>= 1;
    	}
    	return c;
    }
    void Calc()
    {
    	int i, c4;
    	long long c1[N], c2[N] = {0};
    	char c, c3[N];
    	
    	fill(c1, c1 + N, 1);
    	fill(v, v + N, 0);
    	fill(c3, c3 + N, '+');
    	ss.str(s);
    	while (ss >> c)
    	{
    		if (c == '^') ss >> c4;
    		for (i = 0; i < N; i++)
    			switch (c)
    			{
    				case '(': 
    					st[i].push(State(v[i], c1[i], c2[i], c3[i]));
    					v[i] = c2[i] = 0; c1[i] = 1; c3[i] = '+';
    					break;
    				case ')':
    					C = st[i].top(); st[i].pop();
    					if (c3[i] == '+') c2[i] = (v[i] + c1[i] * c2[i] % MOD) % MOD;
    					else c2[i] = (v[i] - c1[i] * c2[i] % MOD) % MOD;
    					c1[i] = C.c1; c3[i] = C.c3; v[i] = C.v;
    					break;
    				case '+':
    				case '-':
    					if (c3[i] == '+') v[i] = (v[i] + c1[i] * c2[i] % MOD) % MOD;
    					else v[i] = (v[i] - c1[i] * c2[i] % MOD) % MOD;
    					c1[i] = 1; c2[i] = 0; c3[i] = c; break;
    				case '*': c1[i] = c1[i] * c2[i] % MOD; c2[i] = 0; break;
    				case '^': c2[i] = Pow(c2[i], c4); break;
    				case 'a': c2[i] = i; break;
    				default: c2[i] = c2[i] * 10 + c - '0';
    			}
    	}
    	for (i = 0; i < N; ++i)
    		if (c3[i] == '+') v[i] = (v[i] + c1[i] * c2[i] % MOD) % MOD;
    		else v[i] = (v[i] - c1[i] * c2[i] % MOD) % MOD;
    	ss.clear();
    }
    
    int main()
    {
    	int n, i, j;
    	bool OK;
    	
    	getline(cin, s);
    	Calc();
    	copy(v, v + N, Std_Value);
    	getline(cin, s);
    	ss.str(s);
    	ss >> n;
    	ss.clear();
    	for (i = 0; i < n; ++i)
    	{
    		getline(cin, s);
    		Calc();
    		OK = true;
    		for (j = 0; j < N; ++j)
    			if ((Std_Value[j] - v[j]) % MOD) {OK = false; break;}
    		if (OK) cout << (char)(i + 'A');
    	}
    	cout << endl;
    	
    	return 0;
    }
    
    

    Pascal :

    var
      s:string;
      h:array[1..100] of int64;
      i,kk:longint;
      ans,n,base,m,p:int64;
    function power(x:int64; y:longint):int64;
    var
      i:longint;
    begin
      x:=x mod maxlongint;
      power:=1;
      for i:=1 to y do power:=power*x mod maxlongint;
    end;
    function data(l,r:longint):int64;
    var
      i:longint;
    begin
      data:=0;
      for i:=l to r do data:=data*10+ord(s[i])-48;
    end;
    function find(l,r:longint):int64;
    var
      i,min:longint;
    begin
      min:=maxlongint;
      find:=0;
      for i:=r downto l do
        if h[i]<min then
        begin
          min:=h[i];
          find:=i;
        end;
    end;
    function opt(a,k,b:int64):int64;
    begin
      case s[k] of
        '+':opt:=(a+b) mod maxlongint;
        '-':opt:=(a-b) mod maxlongint;
        '*':opt:=(a*b) mod maxlongint;
        '^':opt:=power(a,b) mod maxlongint;
      end;
    end;
    function work(l,r:longint):int64;
    var
      a,b:int64;
      k:longint;
    begin
      if s[l]='(' then inc(l);
      if s[r]=')' then dec(r);
      k:=find(l,r);
      if k=0 then exit(data(l,r));
      a:=work(l,k-1);
      b:=work(k+1,r);
      work:=opt(a,k,b);
    end;
    begin
      readln(s);
      p:=pos(' ',s);
      while p<>0 do
      begin
        delete(s,p,1);
        p:=pos(' ',s);
      end;
      n:=length(s);
      for i:=1 to n do
        if s[i]='a' then s[i]:='2';
      for i:=1 to n do h[i]:=maxlongint;
      base:=0;
      for i:=1 to n do
        case s[i] of
          '(':inc(base,3);
          ')':dec(base,3);
          '+','-':h[i]:=base+1;
          '*','/':h[i]:=base+2;
          '^':h[i]:=base+3;
        end;
      ans:=work(1,n) mod maxlongint;
      readln(m);
      for kk:=1 to m do
      begin
        readln(s);
        p:=pos(' ',s);
        while p<>0 do
        begin
          delete(s,p,1);
          p:=pos(' ',s);
        end;
        n:=length(s);
        for i:=1 to n do
          if s[i]='a' then s[i]:='2';
        for i:=1 to n do h[i]:=maxlongint;
        base:=0;
        for i:=1 to n do
          case s[i] of
            '(':inc(base,3);
            ')':dec(base,3);
            '+','-':h[i]:=base+1;
            '*','/':h[i]:=base+2;
            '^':h[i]:=base+3;
          end;
        if ans=work(1,n) mod maxlongint then write(chr(64+kk));
      end;
    end.
    
    • 1

    信息

    ID
    245
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者