首页 > 其他分享 >ACM International Collegiate Programming Contest 2014 A dfs 好题

ACM International Collegiate Programming Contest 2014 A dfs 好题

时间:2023-04-24 22:37:21浏览次数:45  
标签:ch word Contest int Programming 好题 return problem include


GREAT + SWERC = PORTO
We want to have a great SWERC at Porto this year and we approached this challenge
in several ways. We even framed it as a word addition problem, similar to the classic
SEND+MORE=MONEY, where each letter stands for a single digit (0, 1, 2, …, 8, 9)
that makes the arithmetic operation correct. In word additions different letters cannot be
assigned the same digit and the leftmost letter in a word cannot be zero (0). In particular,
a single letter term cannot be zero.
To solve this word addition problem we had to find positive digits for G, S and P, and
digits for R, E, A, T, W, C, O, so that each letter has a different digit and the sum is
correct. It turns out that, unlike the classical SEND+MORE=MONEY which has a single
solution, GREAT+SWERC=PORTO has six solutions.
T=7, E=3, W=9, G=1, A=0, P=4, S=2, C=8, R=6, O=5
T=7, E=3, W=9, G=2, A=0, P=4, S=1, C=8, R=6, O=5
T=8, E=5, W=1, G=3, A=7, P=9, S=6, C=4, R=0, O=2
T=8, E=5, W=1, G=6, A=7, P=9, S=3, C=4, R=0, O=2
T=9, E=5, W=2, G=1, A=8, P=7, S=6, C=4, R=0, O=3
T=9, E=5, W=2, G=6, A=8, P=7, S=1, C=4, R=0, O=3
Having more than one solution does not make GREAT+SWERC=PORTO a good problem
to solve by hand, but it is still a piece of cake for a programer. Moreover, it gives us another
reason to organize SWERC again next year and, who knows, in years to come!
Task
Given a word addition problem, compute the number of solutions (possibly zero).
Input
A line with an integer n, followed by n lines containing a word each with maximum length
of 10 letters. The first n−1 words are the terms to be added and the last line is the result.
Universidade do Porto Computer Science Department 3
Problem A Problem A
Words contain only capital letters. If words have different lengths, they must be interpreted
as aligning to the right. For instance, in the SEND+MORE=MONEY problem, the D of
the first word and E of the second word align with the Y of the final word. You can also
assume that the size of the last word is greater than or equal to the maximum size of the
preceding words, and moreover, at most ten distinct letters are involved in a word problem.
Constraints
3 ≤ n ≤ 10
Each word has at most 10 symbols (capital letters).
A word problem has at most 10 distinct letters.
Output
A single line with an integer: the number of solutions of the word addition problem given
as input.
Sample Input 1
3
GREAT
SWERC
PORTO
Sample Output 1
6
Sample Input 2
3
SEND
MORE
MONEY
Sample Output 2
1
Sample Input 3
5
TOO
GOOD
TO
BE
TRUE
Sample Output 3
93
给定字符串,满足加法,求解有多少种方案,前导0不合法:
dfs 即可: 但写法得细心;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pie acos(-1.0)

ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch>'9') {
        if (ch == '-') {
            f = -1;
        }
        ch = getchar();
    }
    while (ch >= '0'&&ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

ll quickpow(ll a, ll b) {
    ll ans = 1;
    while (b > 0) {
        if (b % 2)ans = ans * a%mod;
        b = b / 2;
        a = a * a%mod;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}
int tot, n, s;
char ch[20][20];
int book[1000], p[1000], a[1000];
int u[1000];
int ans;
int revs(char ch[]) {
    int res = 0;
    int len = strlen(ch);
    if (a[ch[0] - 'A'] == 0)return -1;
    int x = 1;
    for (int i = len - 1; i >= 0; i--) {
        res += x * a[ch[i] - 'A'];
        x = x * 10;
    }
    return res;
}
bool judge() {
    int pre = 0, sum = 0;
    for (int i = 0; i < n - 1; i++) {
        if (revs(ch[i])==-1)return false;
        pre += revs(ch[i]);
    }
    if (revs(ch[n - 1]) == -1)return false;
    sum += revs(ch[n - 1]);
    if (pre == sum)return true;
    else return false;
}

void dfs(int step) {
    if (step >= tot) {
        if (judge())ans++;
        return;
    }
    for (int i = 0; i < 10; i++) {
        if (book[i] == 0) {
            a[p[step]] = i;
            book[i] = 1;
            dfs(step + 1);
            book[i] = 0;
        }
    }
    return;
}
int main() {
    //ios::sync_with_stdio(false);
    while (cin >> n) {
        ms(book); ms(p); ms(a); ms(u);
        tot = 0;
        ans = 0;
    //  step = 0;
        for (int i = 0; i < n; i++) {
            cin >> ch[i];
            int len = strlen(ch[i]);
            for (int j = 0; j < len; j++) {
                u[ch[i][j]-'A'] = 1;
            }
        }
        for (int i = 0; i < 26; i++) {
            if (u[i])p[tot++] = i;
        }
        dfs(0);
        cout << ans << endl;
    }

    return 0;
}


标签:ch,word,Contest,int,Programming,好题,return,problem,include
From: https://blog.51cto.com/u_15657999/6221935

相关文章

  • HDU - 5649 线段树+区间二分答案 (好题)
    DZYhasasequencea[1…n].Itisapermutationofintegers1∼n.Nowhewantstoperformtwotypesofoperations:0lr:Sorta[l…r]inincreasingorder.1lr:Sorta[l…r]indecreasingorder.Afterdoingalltheoperations,hewilltellyouapositionk,andask......
  • [CMU 15-418] (Lecture4) Parallel Programming Basics
    本系列文章为CMU15-418/15-618:ParallelComputerArchitectureandProgramming,Fall2018课程学习笔记课程官网:CMU15-418/15-618:ParallelComputerArchitectureandProgramming参考文章:CMU15-418notes相关资源与介绍:CMU15-418/StanfordCS149:ParallelComput......
  • AtCoder Beginner Contest 158
    AtCoderBeginnerContest158https://atcoder.jp/contests/abc158基础不牢,地动山摇D-StringFormation一个小小的STL应用#include<bits/stdc++.h>#definelllonglongusingnamespacestd;strings;intq,t,f;charc;intmain(){cin>>s>>q......
  • AtCoder Regular Contest 111 F Do you like query problems?
    洛谷传送门AtCoder传送门挺有意思的计数。计数感觉很难做,不妨转成期望,期望又可以转成概率之和。考虑枚举\(w\in[0,m-1]\),把\(>w\)的数设为\(1\),\(\lew\)的数设为\(0\)。那么期望就是所有\(w\),\(a_i\)为\(1\)的概率之和。对于一个\(i\),只有以下的操作能改变\(......
  • SMU Spring 2023 Trial Contest Round 9
    SMUSpring2023TrialContestRound9A-WrongSubtraction#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;constintN=1e5+5,INF=0x3f3f3f3f,Mod=1e6;constdoubleeps=1e-6;typedefl......
  • SMU Spring 2023 Trial Contest Round 9
    A.WrongSubtraction#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){intn,k;cin>>n>>k;while(k--){if(n%10==0)n/=10;elsen--;}cout<<n;return0;}B.T......
  • Atcoder Beginner Contest 299 G
    对于要打印的\(B\),我们首先尝试确定\(B_1\)。让\(f(x)(1≤x≤M)\)是最大的\(i\),使\(A_i=x\)。对于\(r:=\underset{{{1≤x≤M}}}{\min}f(x)\),我们可以证明\(B_1\)是\(A_1,A_2,...,A_r\)中的一个(否则,\(B\)是\(A_{>r}:=(A_r+1,Ar+2,...,A_N)\)的一个子序列,但......
  • AtCoder Beginner Contest 283 Ex Popcount Sum
    洛谷传送门AtCoder传送门记录一下这个神奇的套路。首先有\(\operatorname{popcount}(n)=n-\sum\limits_{i=1}^{\infty}\left\lfloor\frac{n}{2^i}\right\rfloor\)。证一下:\[\operatorname{popcount}(n)=\sum\limits_{i=0}^{\infty}\left\lfloor\dfrac{n}{2^i}\right......
  • AtCoder Regular Contest 115 D Odd Degree
    洛谷传送门AtCoder传送门若连通块是一棵树,考虑钦定\(k\)个点为奇度点,方案数为\(\binom{n}{k}\)。对于叶子,如果它是奇度点,那么连向它父亲的边要保留,否则不保留。这样自底向上考虑,任意一条边的保留情况都可以唯一确定,所以最后方案数就是\(\binom{n}{k}\)。若连通块存在环,仍......
  • Linux shell script programming All In One
    LinuxshellscriptprogrammingAllInOneshell脚本编程Linux系统中登录shell的时候,会从下面的5个启动文件里读取命令;#系统级,所有登录用户都会先启动这个文件$cat/etc/profile#用户级,按照Linux发行版中实际存在的文件个数,依次进行启动$cat$HOME/.bash_pr......