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 << 1);}
template<class T> inline T R(T a)               {return (a << 1 | 1);}
template<class T> inline T lowbit(T a)          {return (a & -a);}
template<class T> inline T Mid(T a,T b)         {return ((a + b) >> 1);}
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 > 0 ? 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 = 1,y = 0,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 >> 1 & 0x55555555) | ((x << 1) & 0xaaaaaaaa); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
    x = (x >> 4 & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
    x = (x >>16 & 0x0000ffff) | ((x <<16) & 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 = 0 ; i < len ; i++) cout<<v[i]<<" ";
        cout<<endl;
    }
    bool operator == (const node &b){
        if(len != b.len) return false;
        for(int i = 0 ; i < len ; i++)
            if(v[i] != b.v[i]) return false;
        return true;
    }
};
vector<int> t;
node now = node(t,0),res = node(t,0);
int n,m;
bool flag;
node fold(node x,int pos){
    int l = x.len;
    vector<int> vx;
    if(pos >= l/2+1) {
        for(int i = 0 ; i < 2*pos-l ; i++){
            vx.push_back(x.v[i]);
        }
        for(int i = 2*pos-l ; i < pos ; i++){
            vx.push_back(x.v[i]+x.v[2*pos-1-i]);
        }
    }else {
        for(int i = l-1 ; i >= 2*pos ; i--){
            vx.push_back(x.v[i]);
        }
        for(int i = 0 ; i < 2*pos ; i++){
            vx.push_back(x.v[i]+x.v[2*pos-1-i]);
        }
    }
    node y = (pos >= l/2+1) ? node(vx,pos) : node(vx,l-pos);
    return y;
}
void dfs(node x,int f){
    //x.show();
    if(flag) return;
    if(x == res){
        flag = 1;
        return;
    }
    if(x.len == 1) return;
    for(int i = 0 ; i < x.len ;i++){
        node xx = fold(x,i);
        if(f == 0 && xx.len == x.len)
            continue;
        else if(f == 1 && xx.len == x.len)
            dfs(xx,0);
        else dfs(xx,1);
    }
}
int main(){
    FIN;
    //FOUT;
    while(scanf("%d",&n)!=EOF){
        flag = false;
        int p;
        vector<int> u,v;
        int sum1 = 0,sum2 = 0;
        for(int i = 0 ; i < n ; i++){
            scanf("%d",&p);
            u.push_back(p);
            sum1 += p;
        }
        scanf("%d",&m);
        for(int i = 0 ; 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,1);
        if(flag){
            printf("S\n");
        }else printf("N\n");
    }
    return 0;
}

 

posted @ 2013-11-04 20:15  Felix_F  阅读(352)  评论(0编辑  收藏  举报