UVA 12649 Folding Machine 搜索
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4378
写了这么长不容易纪念一下
- /********************* Template ************************/
- #include <set>
- #include <map>
- #include <list>
- #include <cmath>
- #include <ctime>
- #include <deque>
- #include <queue>
- #include <stack>
- #include <bitset>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <cassert>
- #include <cstdlib>
- #include <cstring>
- #include <sstream>
- #include <fstream>
- #include <numeric>
- #include <iomanip>
- #include <iostream>
- #include <algorithm>
- #include <functional>
- using namespace std;
- #define EPS 1e-8
- #define DINF 1e15
- #define MAXN 100050
- #define MOD 1000000007
- #define INF 0x7fffffff
- #define LINF 1LL<<60
- #define PI 3.14159265358979323846
- #define lson l,m,rt<<1
- #define rson m+1,r,rt<<1|1
- #define BUG cout<<" BUG! "<<endl;
- #define LINE cout<<" ------------------ "<<endl;
- #define FIN freopen("in.txt","r",stdin);
- #define FOUT freopen("out.txt","w",stdout);
- #define mem(a,b) memset(a,b,sizeof(a))
- #define FOR(i,a,b) for(int i = a ; i < b ; i++)
- #define read(a) scanf("%d",&a)
- #define read2(a,b) scanf("%d%d",&a,&b)
- #define read3(a,b,c) scanf("%d%d%d",&a,&b,&c)
- #define write(a) printf("%d\n",a)
- #define write2(a,b) printf("%d %d\n",a,b)
- #define write3(a,b,c) printf("%d %d %d\n",a,b,c)
- #pragma comment (linker,"/STACK:102400000,102400000")
- template<class T> inline T L(T a) {return (a << );}
- template<class T> inline T R(T a) {return (a << | );}
- template<class T> inline T lowbit(T a) {return (a & -a);}
- template<class T> inline T Mid(T a,T b) {return ((a + b) >> );}
- template<class T> inline T gcd(T a,T b) {return b ? gcd(b,a%b) : a;}
- template<class T> inline T lcm(T a,T b) {return a / gcd(a,b) * b;}
- template<class T> inline T ABS(T a) {return a > ? a : -a;}
- template<class T> inline T Min(T a,T b) {return a < b ? a : b;}
- template<class T> inline T Max(T a,T b) {return a > b ? a : b;}
- template<class T> inline T Min(T a,T b,T c) {return min(min(a,b),c);}
- template<class T> inline T Max(T a,T b,T c) {return max(max(a,b),c);}
- template<class T> inline T Min(T a,T b,T c,T d) {return min(min(a,b),min(c,d));}
- template<class T> inline T Max(T a,T b,T c,T d) {return max(max(a,b),max(c,d));}
- template<class T> inline T exGCD(T a, T b, T &x, T &y){
- if(!b) return x = ,y = ,a;
- T res = exGCD(b,a%b,x,y),tmp = x;
- x = y,y = tmp - (a / b) * y;
- return res;
- }
- template<class T> inline T reverse_bits(T x){
- x = (x >> & 0x55555555) | ((x << ) & 0xaaaaaaaa); x = ((x >> ) & 0x33333333) | ((x << ) & 0xcccccccc);
- x = (x >> & 0x0f0f0f0f) | ((x << ) & 0xf0f0f0f0); x = ((x >> ) & 0x00ff00ff) | ((x << ) & 0xff00ff00);
- x = (x >> & 0x0000ffff) | ((x <<) & 0xffff0000); return x;
- }
- typedef long long LL; typedef unsigned long long ULL;
- //typedef __int64 LL; typedef unsigned __int64 ULL;
- /********************* By F *********************/
- struct node{
- vector<int> v;
- int len;
- node(vector<int> x,int n){
- v = x;
- len = n;
- }
- void show(){
- for(int i = ; i < len ; i++) cout<<v[i]<<" ";
- cout<<endl;
- }
- bool operator == (const node &b){
- if(len != b.len) return false;
- for(int i = ; i < len ; i++)
- if(v[i] != b.v[i]) return false;
- return true;
- }
- };
- vector<int> t;
- node now = node(t,),res = node(t,);
- int n,m;
- bool flag;
- node fold(node x,int pos){
- int l = x.len;
- vector<int> vx;
- if(pos >= l/+) {
- for(int i = ; i < *pos-l ; i++){
- vx.push_back(x.v[i]);
- }
- for(int i = *pos-l ; i < pos ; i++){
- vx.push_back(x.v[i]+x.v[*pos--i]);
- }
- }else {
- for(int i = l- ; i >= *pos ; i--){
- vx.push_back(x.v[i]);
- }
- for(int i = ; i < *pos ; i++){
- vx.push_back(x.v[i]+x.v[*pos--i]);
- }
- }
- node y = (pos >= l/+) ? node(vx,pos) : node(vx,l-pos);
- return y;
- }
- void dfs(node x,int f){
- //x.show();
- if(flag) return;
- if(x == res){
- flag = ;
- return;
- }
- if(x.len == ) return;
- for(int i = ; i < x.len ;i++){
- node xx = fold(x,i);
- if(f == && xx.len == x.len)
- continue;
- else if(f == && xx.len == x.len)
- dfs(xx,);
- else dfs(xx,);
- }
- }
- int main(){
- FIN;
- //FOUT;
- while(scanf("%d",&n)!=EOF){
- flag = false;
- int p;
- vector<int> u,v;
- int sum1 = ,sum2 = ;
- for(int i = ; i < n ; i++){
- scanf("%d",&p);
- u.push_back(p);
- sum1 += p;
- }
- scanf("%d",&m);
- for(int i = ; i < m ; i++){
- scanf("%d",&p);
- v.push_back(p);
- sum2 += p;
- }
- if(sum1 != sum2){
- printf("N\n");
- continue;
- }
- now = node(u,n);
- res = node(v,m);
- dfs(now,);
- if(flag){
- printf("S\n");
- }else printf("N\n");
- }
- return ;
- }
UVA 12649 Folding Machine 搜索的更多相关文章
- UVA.129 Krypton Factor (搜索+暴力)
UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
- UVA - 1630 Folding(串折叠)(dp---记忆化搜索)
题意:给出一个由大写字母组成的长度为n(1<=n<=100)的串,“折叠”成一个尽量短的串.折叠可以嵌套.多解时可输出任意解. 分析: 1.dp[l][r]为l~r区间可折叠成的最短串的长 ...
- uva 11624 Fire!(搜索)
开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...
- 模拟赛20181016 Uva 1040 状压+搜索 2005 ACM world final problem c
题目的隐含条件将这道题指向了最小生成树: 利用类似prim的方法,枚举所有子图并判断是否包含询问点,如果包含那么可以更新答案: 边统计边更新,且由于更新一定是向更多的点状态下更新,所以一定可以统计到答 ...
- uva 707(记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21261 思路:此题需要记忆化搜索,dp[x][y][t]表示当前状 ...
- Power Calculus UVA - 1374 迭代加深搜索
迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大 题目:题目链接 AC代码: #include <i ...
- UVa 10118 记忆化搜索 Free Candies
假设在当前状态我们第i堆糖果分别取了cnt[i]个,那么篮子里以及口袋里糖果的个数都是可以确定下来的. 所以就可以使用记忆化搜索. #include <cstdio> #include & ...
- UVA 1663 Purifying Machine (二分图匹配,最大流)
题意: 给m个长度为n的模板串,模板串由0和1和*三种组成,且每串至多1个*,代表可0可1.模板串至多匹配2个串,即*号改成0和1,如果没有*号则只能匹配自己.问:模板串可以缩减为几个,同样可以匹配原 ...
- UVa 1630 Folding (区间DP)
题意:折叠一个字符串,使得其成为一个尽量短的字符串 例如AAAAAA变成6(A) 而且这个折叠是可以嵌套的,例如 NEEEEERYESYESYESNEEEEERYESYESYES 会变成 2(N5( ...
随机推荐
- poj3134 Power Calculus IDA*
好端端的一道搜索题目,,,硬生生的被我弄成了乱搞题,,,枚举当前的maxd,深搜结果,然而想到的剪枝方法都没有太好的效果,,,最后用一个贪心乱搞弄出来了,,, 贪心:每次必用上一次做出来的数字与其他数 ...
- 洛谷 P2027 bf
P2027 bf 题目描述 bf是一种编程语言,全称为BrainFuck,因为题目名称不能太露骨,所以就简写成bf了. 这种语言的运行机制十分简单,只有一个大小为30000的有符号8位整数(范围[-1 ...
- Qt之QImageWriter
简述 QImageWriter类为写入图像至文件或设备提供了一个独立的接口.QImageWriter支持格式特定的选项(如:质量和压缩率),可以在存储图像之前进行设置.如果不需要这些选项,可以使用QI ...
- zookeeper应用场景练习(分布式锁)
在寻常的高并发的程序中.为了保证数据的一致性.因此都会用到锁.来对当前的线程进行锁定.在单机操作中.非常好做到,比方能够採用Synchronized.Lock或者其它的读写多来锁定当前的线程.可是在分 ...
- PermissionError: [Errno 13] in python
出现该错误,首先尝试以管理员身份运行 cmd.exe 程序,然后关闭所有的与 python 相关的进程. 1. open 打开一个文件夹(目录),而不是文件 这一错误一般发生在使用 open函数对文件 ...
- PostgreSQL Replication之第六章 监控您的设置(2)
6.2 检查pg_stat_replication 检查归档以及 archive_command主要用于即时恢复( PITR,Point-In-Time- Recovery).如果您想监控一个基于流的 ...
- webService接口发布失败问题
今天在原有工程上新增加了个webService接口的服务类,但是总提示 axis2 出错 File "/axis2-web/listSingleService.jsp" not f ...
- WinDBG help
WinDBG is a great, free tool. It is more powerful than Visual Studio's built-in debugger, but is har ...
- Python爬虫基础示例
使用pip安装相关依赖: pip install requests pip install bs4 安装成功提示:Successfully installed *... 爬取中国天气网数据示例代码: ...
- OpenJDK源码研究笔记(八)-详细解析如何读取Java字节码文件(.class)
在上一篇OpenJDK源码研究笔记(七)–Java字节码文件(.class)的结构中,我们大致了解了Java字节码文件的结构. 本篇详细地介绍了如何读取.class文件的大部分细节. 1.构造文件 ...