字符串 || Gym 101653T Runes

给T个式子,其中有一个数字是?,让确定?代表哪个数字

负号,前导0,出现过的数字不能是?

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
char s[33];
int vis[11];
bool checkzero(int t)
{
    if(s[t] == ‘-‘) t++;
    if((s[t] == ‘?‘) && ((s[t+1] >= ‘0‘ && s[t+1] <= ‘9‘) || s[t+1] == ‘?‘) ) return 1; //no
    return 0;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%s", s);
        int len = strlen(s);
        long long a, b, c;
        memset(vis, 0, sizeof(vis));
        int op, idx, zf;
        for(int i = 0; i < len; i++)
        {
            if(s[i] >= ‘0‘ && s[i] <= ‘9‘) vis[s[i] - ‘0‘] = 1;//等式中出现过的数不能是?
            if(s[i] == ‘=‘) idx = i;
        }
        int ans, flag = 0;
        for(int j = 0; j <= 9; j++)//枚举?的所有可能情况
        {
            if(vis[j] == 1) continue;
            a = 0, b = 0, c = 0;
            zf = 1;//负号坑
            for(int i = 0; i < len; i++)
            {
                if(s[i] == ‘-‘ && i == 0) zf = -1;
                else if(s[i] == ‘+‘ || s[i] == ‘-‘ || s[i] == ‘*‘)
                {
                    op = i;
                    break;
                }
                else if(s[i] == ‘?‘) a = a * 10 + j;
                else a = a * 10 + s[i] - ‘0‘;
            }
            if(j == 0 && checkzero(0)) continue;
            a = a * zf;
            zf = 1;
            for(int i = op + 1; i < idx; i++)
            {
                if(s[i] == ‘-‘ && i == op+1) zf = -1;
                else if(s[i] == ‘?‘) b = b * 10 + j;
                else b = b * 10 + s[i] - ‘0‘;
            }
            if(j == 0 && checkzero(op+1)) continue;
            b = b * zf;
            zf = 1;
            for(int i = idx + 1; i < len; i++)
            {
                if(s[i] == ‘-‘ && i == idx+1) zf = -1;
                else if(s[i] == ‘?‘) c = c * 10 + j;
                else c = c * 10 + s[i] - ‘0‘;
            }
            if(j == 0 && checkzero(idx+1)) continue;
            c = c * zf;
            if(s[op] == ‘+‘ && a + b == c) {ans = j; flag = 1; break;}
            if(s[op] == ‘-‘ && a - b == c) {ans = j; flag = 1; break;}
            if(s[op] == ‘*‘ && a * b == c) {ans = j; flag = 1; break;}
        }
        if(flag) cout << ans << endl;
        else cout << "-1" << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/pinkglightning/p/8978024.html

时间: 2024-11-05 22:41:45

字符串 || Gym 101653T Runes的相关文章

三年开发经验,从小厂离职后,我凭什么拿到了阿里、腾讯、今日头条的offer

临近年末,很多程序员心里大概都准备着年后找工作或者跳槽.最近也有很多人都在交流群里求大厂面试题.刚好我今年从小型软件公司离职,奋战4个多月成功拿到了阿里.腾讯.今日头条的offer.讲述面经的文章很多,本篇文章主要记录分享我的面试前的准备过程,涵盖了辞职原因.准备过程.面试刷题.以及简历制作与投递. 我为什么离职 关于离职原因,马云有一句经典的话“要么钱没给到位,要么心委屈了”,想必大家耳熟能详了,我这里再细说一下我个人离职原因: 工资倒挂,涨薪不如意 在之前的小厂薪资倒挂现象严重,新入职的员工

ACM: Gym 100935B Weird Cryptography - 简单的字符串处理

Weird Cryptography Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935B Description standard input/output Khaled was sitting in the garden under an apple tree, suddenly! , well... you should guess what happened, an

Gym 100952A&amp;&amp;2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】

A. Who is the winner? time limit per test:1 second memory limit per test:64 megabytes input:standard input output:standard output A big marathon is held on Al-Maza Road, Damascus. Runners came from all over the world to run all the way along the road

codeforces gym 101164 K Cutting 字符串hash

题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A: 思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N: 题目链接:传送门 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #

Gym - 101908H Police Hypothesis (树链剖分+字符串哈希)

题意:有一棵树,树上每个结点上有一个字母,有两种操作: 1)询问树上两点u,v间有向路径上有多少个字母和某个固定的字符串相匹配 2)将结点u的字母修改为x 树剖+线段,暴力维护前缀和后缀哈希值(正反都要维护)以及区间内匹配的个数,合并两区间时判断一下跨过分界点的情况就行了.由于被匹配的字符串长度不超过100,所以最多只需维护长度为100的前缀/后缀. 但即使这样复杂度也足足有$O(100nlog^2n)$啊,这常数是得有多小才能过掉... 注意各种条件判断和细节处理,还有就是这题内存比较吃紧,使

【枚举】【字符串哈希】Gym - 101164K - Cutting

给你A B两个串,让你切B串两刀,问你能否把切开的三个串拼成A. 哈希显然. #include<cstdio> #include<cstring> using namespace std; typedef unsigned long long ull; const ull MOD1=2000000011; const ull MOD2=1000000007; const ull base1=10007; const ull base2=10009; int n; char a[10

Gym 100989E 字符串

Description standard input/output Islam is usually in a hurry. He often types his passwords incorrectly. He hates retyping his password several times whenever he tries to login, especially that his passwords are usually very long. He believes that we

【字符串+BFS】Problem 7. James Bond

https://www.bnuoj.com/v3/external/gym/101241.pdf [题意] 给定n个字符串,大小写敏感 定义一个操作:选择任意m个串首尾相连组成一个新串 问是否存在一个这样的串s,s可以由不同的串首尾相连得到 最多100个字符串,所有字符串的总长度不超过5000 [样例解释] aB5可以由a+B5得到,也可以由aB+5得到,所以输出YES [思路] 首先一定是在100个选择2个串a,b,a是b的前缀 然后a和b的前缀可以消去,我们想知道b剩下的右半部分是哪一个串的

Codeforces Gym 100570 E. Palindrome Query Manacher

E. Palindrome QueryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100570/problem/E Description De Prezer loves palindrome strings. A string s1s2...sn is palindrome if and only if it is equal to its reverse. De Prezer also love