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 ...
随机推荐
- HDU-1035 Robot Motion
http://acm.hdu.edu.cn/showproblem.php?pid=1035 Robot Motion Time Limit: 2000/1000 MS (Java/Others) ...
- Supervisord管理
原文地址:http://blog.csdn.net/fyh2003/article/details/6837970 学习笔记 Supervisord可以通过sudo easy_install supe ...
- [转]让程序在崩溃时体面的退出之SEH+Dump文件
原文地址:http://blog.csdn.net/starlee/article/details/6649605 在我上篇文章<让程序在崩溃时体面的退出之SEH>中讲解了SEH中try/ ...
- 【转】CPU调度
转自:http://blog.csdn.net/xiazdong/article/details/6280345 CPU调度 用于多道程序 以下先讨论对于单CPU的调度问题. 回顾多道程序,同时把 ...
- thinkphp I方法取传参
/** * 获取输入参数 支持过滤和默认值 * 使用方法: * <code> * I('id',0); 获取id参数 自动判断get或者post * I('post.name','','h ...
- 在Tomcat下部署web项目
每个web项目可以以两种方式存在,如联合风控项目,Urc.war,另一中是Urc解压后的目录结构.而tomcat目录下的\webapps\下则均是要部署的web项目解压后的文件夹,启动tomcat后, ...
- winfrom 操作 INI 文件 分类: WinForm 2014-07-22 12:49 156人阅读 评论(0) 收藏
<strong><span style="font-size:18px;">(1)INI文件的名称:FileConfig.ini</span>& ...
- jquery 手机 图片切换 例子 网址
http://m.swdhy.com/page/ShowCompany.aspx?cid=388481&name=山东潍坊金城服装有限公司
- hdoj 3157 Crazy Circuits 【有下界最小流】
题目:hdoj 3157 Crazy Circuits 题意:如今要制造一个电路板.电路板上有 n 个电子元件,各个元件之间有单向的电流流向.然后有一个 + .电流进入, -- 电流汇入,然后推断能不 ...
- [CSS] Transforms
Degrees and Turns Degrees are just one value that can be set to a rotate transform to determine how ...