【算法刷题】poj 2192 Zipper
2013-09-30 11:10
495 查看
http://poj.org/problem?id=2192
两种 深搜剪枝or动规
以下代码使用动规方法。
做了两题动规(poj 2192 & poj 1080),觉得关键在找到状态转移的方法和边界的处理上。
两种 深搜剪枝or动规
以下代码使用动规方法。
做了两题动规(poj 2192 & poj 1080),觉得关键在找到状态转移的方法和边界的处理上。
// // main.cpp // proj // // Created by yi chen on 13-10-10. // Copyright (c) 2013年 yi chen. All rights reserved. //POJ 2192 Zipper #include <iostream> #include <string> using namespace std; bool isZipper(string & s1, string & s2, string & s3){ int mapRows=(int)s1.length()+1, mapCols=(int)s2.length()+1; bool **map=new bool* [mapRows]; for (int i=0; i<mapRows; i++) { map[i]=new bool[mapCols]; memset(map[i], 0, sizeof(bool)*mapCols); } map[0][0]=true; //边界处理 for (int i=1; i<mapRows; i++) { if (map[i-1][0] && s1[i-1]==s3[i-1]) { map[i][0]=true; }else{ map[i][0]=false; } } for (int j=1; j<mapCols; j++) { if (map[0][j-1] && s2[j-1]==s3[j-1]) { map[0][j]=true; }else{ map[0][j]=false; } } //状态转移 for (int i=1; i<mapRows; i++) { for (int j=1; j<mapCols; j++) { if (map[i][j-1] && s2[j-1]==s3[i+j-1]) { //左边的状态 map[i][j]=true; }else if (map[i-1][j] && s1[i-1]==s3[i+j-1]) {// 上面的状态 map[i][j]=true; }else{ map[i][j]=false; } } } bool res=map[s1.length()][s2.length()]; for (int i=0; i<mapRows; i++) { delete [] map[i]; } delete [] map; return res; } int main() { int numOfCases = 0; string s1,s2,s3; cin>>numOfCases; for (int i=1; i<=numOfCases; i++) { cin>>s1; cin>>s2; cin>>s3; bool res=isZipper(s1,s2,s3); cout<<"Data set "<<i<<": "; if(res){ cout<<"yes"<<endl; }else{ cout<<"no"<<endl; } } return 0; }
相关文章推荐
- 动易2006序列号破解算法公布
- C#数据结构与算法揭秘二
- 浅析STL中的常用算法
- JavaScript 组件之旅(二)编码实现和算法
- 将15位身份证补全为18位身份证的算法示例详解
- C++算法系列之日历生成的算法代码
- 1 2 3 4 5 6 7 8 9 = 110的java实现
- Sedgewick之巨著《算法》,与高德纳TAOCP一脉相承
- 【代码】Pythonの代码片段
- STL中算法
- 数据结构&算法学习
- 算法的时间复杂度
- 算法导论:选择排序的原理与实现
- PHP实现四种常用的排序算法
- 图解插入排序算法
- 一些常见算法的JavaScript实现
- 平方根sqrt()函数的底层算法效率
- 二叉搜索树的一些相关算法介绍
- 欧几里德算法(辗转相处法)练手
- 面试中常见的一些算法问题