sgu170:Particles
来源:互联网 发布:scientific linux 吧 编辑:程序博客网 时间:2024/05/29 04:46
题意:
给出一个二进制串,每次操作可以把相邻的0和1交换,求从原串到目标串的最少操作数,无解输出-1。
分析:
分析一下就是把src串中的1移到aim串的相应位上的最少步数。
①长度不相等或1数量不相等,输-1;
②记录pos[0],pos[1]为src,aim中第k个1的位置,直接累加答案。
讲的很简单,直接看代码就好。
给出一个二进制串,每次操作可以把相邻的0和1交换,求从原串到目标串的最少操作数,无解输出-1。
分析:
分析一下就是把src串中的1移到aim串的相应位上的最少步数。
①长度不相等或1数量不相等,输-1;
②记录pos[0],pos[1]为src,aim中第k个1的位置,直接累加答案。
讲的很简单,直接看代码就好。
#include <cstdio>#include <cmath>#include <cstring>using namespace std;const int MAXL = 5005;char s1[MAXL], s2[MAXL];int l1, l2;int pos[2][MAXL], top[2];int main(){scanf("%s\n%s\n", s1, s2);l1 = strlen(s1);l2 = strlen(s2);if(l1 != l2) puts("-1");else {int a = 0, b = 0;for(int i = 0; i < l1; ++i){if(s1[i] == '+') pos[0][a++] = i;if(s2[i] == '+') pos[1][b++] = i;}if(a != b)puts("-1");else{int ans = 0;for(int i = 0; i < a; ++i)ans += abs(pos[0][i]-pos[1][i]);printf("%d\n", ans);}}return 0;}
0 0
- sgu170:Particles
- Particles System
- 170. Particles
- particles.js
- 粒子滤波 particles Filter
- sgu 170 Particles
- NYOJ 318 Special particles
- nyoj 318 Special particles
- sgu 170 Particles
- 粒子效果 Particles
- particles.js 使用
- particles system学习
- cocos2d-x 基本概念 粒子(Particles)
- particles.js中文开发手册
- “High-Speed, Off-Screen Particles” in Unity
- 超炫背景粒子插件particles.js
- Qt5官方demo解析集13——Qt Quick Particles Examples - Image Particles
- 运用Soft Particles解决 硬边缘( intersection of a 'hard')
- 卡尔曼滤波器
- sicily1031-Campus(单源最短路径dijkstra)
- 10、RTC实时时钟实验
- hdu 2586 How far away ?(LCA离线)
- SQL2043N 与 linux的randomize_va_space特性
- sgu170:Particles
- IIS7.5上的REST服务的Put操作发生HTTP Error 405.0 - Method Not Allowed
- 管理员权限、进程权限与UAC
- 【Leetcode】Reorder List
- XML解析
- Java—String与StringBuffer的区别
- thinkphp3.2的模板渲染LAYOUT_ON,特定文件不加载公共文件
- jquery实现的固定位置下拉隐藏上拉显示悬浮导航菜单特效
- error: Error retrieving parent for item: No resource found that matches the given name 'android:Win