1st

· · 题解

简化题意

找到一个 ? 使字符串左右元素守恒,且 ? 属于题目背景研究的物质范围内。

解题思路

思路总结

代码呈现

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cctype>
#include<cstdlib>
#include<functional>
#include<istream>
#include<sstream>
#include<streambuf>
#define ll long long 
using namespace std;
const int N=26;
int a[N];
int main()
{
    int n,m,c;
    string s;
    bool b,d,e;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        b=false;//重置标记变量和数组 
        d=false;
        e=false;
        memset(a,0,sizeof(a));
        cin>>s;
        s[s.length()]='1';//防止越界 
        for(int j=0;j<s.length();j++)
        {
            if(s[j]=='=')//判断是否到达方程右侧 
                b=true;
            else
            {
                if(b==false)
                {
                    if(s[j]=='?')// ?在方程左侧 
                        c=-1;
                    else if(s[j]>='A'&&s[j]<='Z')
                    {
                        if(s[j+1]>='0'&&s[j+1]<='9')
                            a[(int)(s[j]-'A')]+=(int)(s[j+1]-'0');
                        else    
                            a[(int)(s[j]-'A')]++;
                    }
                }
                else
                {
                    if(s[j]=='?')// ?在方程右侧 
                        c=1;
                    else if(s[j]>='A'&&s[j]<='Z')
                    {
                        if(s[j+1]>='0'&&s[j+1]<='9')
                            a[(int)(s[j]-'A')]-=(int)(s[j+1]-'0');
                        else    
                            a[(int)(s[j]-'A')]--;
                    }
                }
            }
        }
        for(int j=0;j<26;j++)//是否无法配平 
            if(a[j]*c<0||a[j]*c>9)
            {
                printf("No Solution");
                d=true;//标记 
                break;
            }
        if(d==false)
        {
            for(int j=0;j<26;j++)//寻找缺失元素 
            {
                if(a[j]*c==1)
                {
                    printf("%c",(char)(j+'A'));
                    e=true;
                }
                else if(a[j]*c>1)
                {
                    printf("%c%d",(char)(j+'A'),a[j]*c);
                    e=true;
                }
            }
            if(e==false)//特判,第一种情况中的特殊情况 
                printf("No Solution");
        }
        printf("\n");
    }
    return 0;
}