POJ-3158
来源:互联网 发布:淘宝刷直通车是黑车 编辑:程序博客网 时间:2024/04/29 08:46
题意理解:
平移两个字符串,要求两个字符串的对应位置不能同时出现两个‘2’;
解题思路:
假设字符串str1和str2,
1. 一般的思路是:从str1[n]与str2[0]做比较开始,然后str1不断向右平移,做比较;
2. 等价思路是:
先将str1看作bottom,str2看作top,从str1[0]和str2[0]开始做比较,str2不断向右平移;
然后反过来,str2看作bottom,str1看作top,重复上面的过程;
最后,比较两种方式的最有解;
解题代码:
#include<iostream>#include<string>#include<math.h>using namespace std;int process(string s1, string s2){int len1=s1.length();int len2=s2.length();for(int i=0;i<len1;i++){bool isMatch=true;for(int j=0;j<min(len1-i,len2);j++){if(s1[i+j]=='2' && s2[j]=='2'){isMatch=false;break;}}if(isMatch){return max(i+len2,len1);}}return len1+len2;}int main(int argc, char *argv[]){string str1;cin>>str1;string str2;cin>>str2;int minLen=min(process(str1,str2),process(str2,str1));cout<<minLen<<endl;return 0;}
0 0
- POJ-3158
- poj 3158 Kickdown
- POJ 3158 Kickdown
- POJ 3158 Kickdown 笔记
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- 零基础unity3d游戏开发系列目录
- 字符串集合求并集
- android 监听wifi连接
- git学习总结(2)基本操作
- TCP/IP详解卷1 读书笔记:第二十六章 Telnet协议
- POJ-3158
- ios内存管理&1之常见的内存管理
- poj2406 Power Strings
- STM32标准库文件使用
- PHP Ajax 跨域问题最佳解决方案
- 我的delphi笔记-来自为知笔记
- mysql中的定义条件和处理程序
- StringBuilder源码分析
- 链表结点的逆置