2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS
基爷与加法等式
Time Limit: 20 Sec Memory Limit: 256 MB
题目连接
http://acm.uestc.edu.cn/#/contest/show/61
Description
一天,上小学的妹妹跑过来问基爷一道字母加法等式,基爷不假思索的便给出了一组可行解。
聪明的你发现,一个字母等式可能有多种不同解,于是你想编个程序计算一下
Input
每组数据第一行一个整数n,表示有n个字符串 3 ≤ n ≤ 10
接下来n行,每行有1个最多只含10个大写字母的字符串,前 n - 1 行的字符串表示加数,第 n 行的字符串表示和
每个样例最多只有10个互不相同的大写字母,每个字母表示 0 - 9 中的一个数,相同字母表示相同的数,不同字母表示不同的数输入包含多组数据。
每组数据第一行一个整数n,表示有n个字符串 3 ≤ n ≤ 10
接下来n行,每行有1个最多只含10个大写字母的字符串,前 n - 1 行的字符串表示加数,第 n 行的字符串表示和
每个样例最多只有10个互不相同的大写字母,每个字母表示 0 - 9 中的一个数,相同字母表示相同的数,不同字母表示不同的数
Output
对于每组数据输出一个整数,表示不同的可行解的数量。
对于两个可行解,只要有一个字母表示不同的数字,我们就认为这两个可行解不同
Sample Input
TAI
SHEN
LA
ACER
3
SEND
MORE
MONEY
Sample Output
1
HINT
如果各个字符串长度不等,右对齐后再运算每个字符串最左边的字母表示的数字不能为0不保证最后一个字符串的长度大于等于前面的表示加数的字符串长度
题意
题解:
直接爆搜就好了,唯一要注意的就是从低位开始搜
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int mp[];
int H[];
string s[];
int n,tmp;
char k[];
int vis[],ans=,ma;
int tp[];
int ans1[];
int ans2[];
int check()
{
ans1[ma]=;
for(int i=ma-;i>=;i--)
{
ans1[i]=;
ans2[i]=;
for(int j=;j<n-;j++)
{
if(H[s[j][i]-'A']==-)
return ;
ans1[i]+=H[s[j][i]-'A'];
}
ans1[i]+=(ans1[i+]/);
if(H[s[n-][i]-'A']==-)
return ;
ans2[i]+=H[s[n-][i]-'A'];
if((ans1[i]%)!=ans2[i])
return ;
}
if(ans1[]>)
return ;
return ;
}
void dfs(int x)
{
if(x==tmp)
{
ans++;
return;
}
for(int i=;i>=;i--)
{
if(i==&&tp[k[x]-'A'])
continue;
if(vis[i])
continue;
vis[i]=;
H[k[x]-'A']=i;
if(!check())
dfs(x+);
vis[i]=;
H[k[x]-'A']=-;
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<;i++)
mp[i]=tp[i]=H[i]=;
ma=;
for(int i=;i<n;i++)
cin>>s[i],ma=max(ma,(int)s[i].size());
for(int i=;i<n;i++)
tp[s[i][]-'A']=;
for(int i=;i<n;i++)
{
int len=s[i].size();
for(int j=;j<ma-len;j++)
{
s[i]='['+s[i];
}
}
tmp=;
ans=;
for(int i=ma-;i>=;i--)
{
for(int j=;j<n;j++)
{
if(mp[s[j][i]-'A'])
continue;
if(s[j][i]=='[')
continue;
k[tmp++]=s[j][i];
mp[s[j][i]-'A']=;
}
}
for(int i=;i<tmp;i++)
H[k[i]-'A']=-; H['['-'A']=;
dfs();
printf("%d\n",ans);
}
}
2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS的更多相关文章
- 2015 UESTC 搜索专题D题 基爷的中位数 二分
基爷的中位数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descr ...
- 2015 UESTC 搜索专题N题 韩爷的梦 hash
韩爷的梦 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descrip ...
- 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜
Eight Puzzle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...
- 2015 UESTC 搜索专题E题 吴队长征婚 爆搜
吴队长征婚 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descri ...
- 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp
秋实大哥の恋爱物语 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...
- 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索
邱老师降临小行星 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...
- 2015 UESTC 搜索专题M题 Palindromic String 马拉车算法
Palindromic String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/s ...
- 2015 UESTC 搜索专题J题 全都是秋实大哥 kmp
全都是秋实大哥 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...
- 2015 UESTC 搜索专题A题 王之迷宫 三维bfs
A - 王之迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...
随机推荐
- SQL server(到主机的TCPIPl连接失败的问题)
1 首先要做的是在sql新建查询里输入 exec sys.sp_readerrorlog 0, 1, 'listening' 运行后 会显示你的sql 正在运行的tcp/ip接口 看看是否和你java ...
- 美团实习Java岗面经,已拿offer
作者:icysnowgx 链接:https://www.nowcoder.com/discuss/71954?type=2&order=3&pos=10&page=1 来源:牛 ...
- Java源码-HashMap(jdk1.8)
一.hash方法 如下是jdk1.8中的源码 static final int hash(Object key) { int h; return (key == null) ? 0 : (h = ke ...
- inet_confirm_addr && confirm_addr_indev
确认给定参数范围的ip地址是否存在: /* * Confirm that local IP address exists using wildcards: * - net: netns to chec ...
- 用Centos7搭建小微企业Samba文件共享服务器【转】
转自 用Centos7搭建小微企业Samba文件共享服务器 - 今日头条(www.toutiao.com)http://www.toutiao.com/i6436937837660078593/ 最近 ...
- 生成器(generator)和迭代(iterable , iterator, iteration)
在搞清楚Generator之前,我们先讨论一下 iterable , iterator, iteration 1.Iterable 我们知道,在Python中所有东西都是object, 比如说变量,容 ...
- C/C++——C语言库函数大全
本文转载自:https://blog.csdn.net/yanfan0916/article/details/6450442###; 1. 分类函数: ctype.h int isalpha(int ...
- leetcode 之Remove Duplicates from Sorted Array(2)
描述 Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice? For exampl ...
- log优化
isLoggable(Level level) 包含计算的日志记录用isLoggable判断下. debug info warn error ,一般记录error, 但是其他里面的计算还是 ...
- webuploader插件使用分析
大致架构: 前端:html5+ajax 后端:java (struts框架相关) 碰到问题: 后台coder给我提供一个接口./file/uploader.do?upFile=?,让我上传文件对应up ...