SGU 170.Particles
Solution:
这其实是道很简单的题。
只要从一端开始,以‘+’或‘-’,任意一种开始找,找到与目标串最近的相同字符的距离就是需要交换的次数。
+++——— 对齐第一个‘-’ —+++——
------------>
———+++ ———+++
以‘-’为例,不断向一端交换的话,最后交换的其实只要当前的‘-’和一个‘+’的位置,其他‘-’的相对位置不变。
因此只要从左到右匹配第i个‘-’,计算出它们的距离差,累加即可。
code:
#include <iostream>
#include <string>
#define abs(x) (max(x,-(x)))
using namespace std;
const int INF = 5555;
int len, a[INF], b[INF], n1, n2, ans;
string st;
int main() {
cin >> st;
len = st.size();
for (int i = 0; i < len; i++)
if (st[i] == '-') a[++n1] = i;
cin >> st;
for (int i = 0, t = 0; i < len; i++)
if (st[i] == '-') b[++n2] = i;
if (n1 != n2) {
cout << -1;
return 0;
}
for (int i = 1; i <= n1; i++)
ans += abs (a[i] - b[i]);
cout << ans;
}
SGU 170.Particles的更多相关文章
- SGU 170 Particles(规律题)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=170 解题报告:输入两个由'+'和'-'组成的字符串,让你判断第二个串能不能由第一个 ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- 今日SGU 5.8
SGU 109 题意:一个n*n的矩形,起点在1,1然后每次给你一个操作,走ki步,然后你可以删除任意一个点这次步走不到的,删了就不能再走了,然后问构造这种操作,使得最后删除n*n-1个点 剩下一个点 ...
- SGU Volume 1
SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...
- Interpolation particles In Katana
I write the sphere radius interpolation for katana plugin that can transfer attributes,render attrib ...
- 弄个知乎的粒子动态背景_实践particles.js
好久没登录知乎,发现他们的登录页面粒子动态效果蛮炫的,查一下代码用了Particles.js基于Canvas画布创建粒子颗粒效果. 上图 上图: 感觉有比格,就照着弄了一个,玩玩. githu ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- NYOJ题目170网络的可靠性
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAs8AAANvCAIAAACte6C6AAAgAElEQVR4nOydPbLcNhOu7yaUayGOZy
- ACM: SGU 101 Domino- 欧拉回路-并查集
sgu 101 - Domino Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Desc ...
随机推荐
- Unity3d 屏幕空间人体皮肤知觉渲染&次表面散射Screen-Space Perceptual Rendering & Subsurface Scattering of Human Skin
之前的人皮渲染相关 前篇1:unity3d Human skin real time rendering 真实模拟人皮实时渲染 前篇2:unity3d Human skin real time ren ...
- [git] git 的基础功能
有两种方法获得一个 git 仓库:自行初始化,克隆别人已有的仓库 自行初始化 git init 克隆别人已有的库 git clone git@github.com:garrisonz/gitStudy ...
- Windows宿主机访问Ubuntu中mysql数据库笔记
1.网络 既然要远程ubuntu的系统.那么首先是两个网络是不是在一个网段.能不能ping的通? a) Windows电脑上--cmd 打开命令窗口.键入:ipconfig 命令.查看主机IP. b ...
- editplus 使用小技巧
1 editplus怎么设置tab键跳的字符数? Tools -> Preferences -> Files -> Settings & syntax -> Tab/I ...
- UVa11925 Generating Premutations
留坑(p.254) #include<cstdio> #include<cstring> #include<cstdlib> #include<algorit ...
- SQL string类型的数据按int类型排序 分类: SQL Server 2014-12-08 16:56 393人阅读 评论(0) 收藏
说明: 我在做wms进销存软件时,发现一个问题:一张入库单(T_OutIn_BoxTop),入库扫描时要分成多箱,箱号(BoxTop_No)可以是数字也可以是字符串,所以箱号只能是字符串类型的,问题来 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(38)-Easyui-accordion+tree漂亮的菜单导航
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(38)-Easyui-accordion+tree漂亮的菜单导航 系列目录 本节主要知识点是easyui ...
- shell 获取网关 以及修改ip 启用网卡
shell 获取网关 以及修改ip 启用网卡 #!/bin/bash #autho freefei #script is a init computer eth #data 2014 10 09 19 ...
- gitlab 启动参考
第一步 启动gitlab bundle exec rails s -e production -d 第二步 启动redis /etc/init.d/redis start chkconfig redi ...
- android 48 广播
系统开始重启会发送开机重启广播,电量低的时候会发送电量低的广播,广播注册有2种:系统说明文件xml注册和Java代码注册,前者是静态注册(全局注册)后者是动态注册(依赖于当时组建,组件销毁就收不到广播 ...