250:

给一个串S,可以做这样的操作,可以将串中的一种字母变成另一种字母,代价是该种字母的数量。求解的问题是,最小的代价将串S变成回文串。

根据回文关系,我们可以形成等价对应关系,a与b等价对应说明a和b必须是同种字母,根据这个关系,我们可以得到一个图,每个连通块表示要变成一种相同的字母,而这个操作的最小代价就是将连通块中除出现次数最多的字母全部都转变成出现次数最多的字母。

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std; bool mm[][];
int cnt[];
bool used[]; class GooseTattarrattatDiv1 {
public:
int getmin(string S) {
memset(cnt, , sizeof(cnt));
memset(mm, false, sizeof(mm));
memset(used, false, sizeof(used));
int n = S.size();
for (int i = ; i < n; i++) {
mm[S[i]][S[n - - i]] = true;
mm[S[n - - i]][S[i]] = true;
cnt[S[i]]++;
}
int res = ;
for (int i = ; i < ; i++) {
int sum = , max = ; dfs(i, sum, max);
res += (sum - max);
}
return res;
}
void dfs(int i, int& sum, int& max) {
used[i] = true;
sum += cnt[i];
if (cnt[i] > max) max = cnt[i]; for (int j = ; j < ; j++) {
if (mm[i][j] && used[j] == false) {
dfs(j, sum, max);
}
}
}
};

500:

有R,G,B三种齿轮,不同颜色齿轮之间不会相接,相接的关系通过graph给出,同种颜色齿轮旋转方向相同,旋转方向一共有两种:顺时针与逆时针。求解如何去掉最少的齿轮使得该graph能相容,即可以找到某种旋转顺序使得不会发生矛盾(矛盾指相接的两个齿轮旋转方向相同)。

同种颜色的点归于一个集合,因此我们有三个集合,R,G,B,集合内的点不会有边,这个图由三个二分图组成,因为有3个集合,枚举一下齿轮的旋转方向,一共有三种情况:RG同向,GB同向,BR同向。于是问题转换成这三种情况中旋转最小值,对于每个情况,去掉最少的齿轮使其不矛盾便是一个最小点覆盖问题,二分图的最小点覆盖可以转变成二分图的最大匹配问题。

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std; class GearsDiv1 {
private:
vector<int> left, right;
vector<string> G;
bool s[];
int link[];
public:
int getmin(string color, vector <string> graph) {
vector<int> r, g, b;
for (int i = ; i < color.length(); i++) {
if (color[i] == 'R') r.push_back(i);
else if (color[i] == 'G') g.push_back(i);
else if (color[i] == 'B') b.push_back(i);
}
int rg = max_bimatch(r, g, graph);
int gb = max_bimatch(g, b, graph);
int br = max_bimatch(b, r, graph);
return min(rg, min(gb, br));
}
int max_bimatch(vector<int> l, vector<int> r, vector<string> graph) {
//
left = l; right = r; G = graph; int res = ;
memset(link, -, sizeof(link));
for (int i = ; i < left.size(); i++) {
memset(s, false, sizeof(s));
if (dfs(i)) res++;
} return res;
}
bool dfs(int i) {
for (int j = ; j < right.size(); j++) {
if (G[left[i]][right[j]] == 'Y' && s[right[j]] == false) {
s[right[j]] = true;
if (link[right[j]] == - || dfs(link[right[j]])) {
link[right[j]] = i;
return true;
}
}
}
return false;
}
};

SRM589的更多相关文章

  1. SRM589 DV1 250 回文字符串

    其实这道题挺简单的,不过刚开始我钻了一个错误的死胡同.想明白之后才发现. 题目要求用最少的时间来将一个字符串变成回文字符串.(具体题目参看topcoder srm589 DV1 250分值的题目,不便 ...

随机推荐

  1. 视酷即时通讯系统应用源码 V1.0

    视酷即时通讯系统(原创),成熟稳定,拥有和微信一样强大的功能不再是梦,节省几个月研发时间迅速融合进项目中: 1.首家支持聊天室群聊 2.支持和微信一样的语音聊天,可以显示时长.未读状态,自动轮播未读语 ...

  2. webuploader上传插件

    一:官网 http://fex.baidu.com/webuploader/ 二:示例

  3. myeclipse 8.5 破解

    步骤: (1)新建一个java project项目 (2)在src目录下新建一个名为MyEclipseGen的Java文件(MyEclipseGen.java) (3)MyEclipseGen.jav ...

  4. Jquery get parameter value

    http://www.sitepoint.com/url-parameters-jquery/ $.urlParam('id') ==> $.urlParam = function(name){ ...

  5. 我的第一个canvas的作品:漫画对白编辑器

    背景:一直都对canvas挺有有兴趣的,之前刚刚看了<HTML5 CANVAS基础教程>,写了篇读书笔记. 起因:老婆发来一张最近比较热的漫画图(友谊的小船说翻就翻什么的).这种漫画,经常 ...

  6. Using jQuery to add a dynamic “Back To Top” floating button with smooth scroll

    Ever read a really long blog post or article and then had to scroll all the way up to the top of the ...

  7. 超棒的阿里巴巴矢量图标库——支持IE6

    Iconfont.cn 是由阿里巴巴UX部门推出的矢量图标管理网站,也是国内首家推广Webfont形式图标的平台.网站涵盖了1000多个常用图标并还在持续更新 中,Iconfont平台为用户提供在线图 ...

  8. python编程语言缩进格式

    python的缩进格式是python语法中最特别的一点,很多已经习惯了其他语言的朋友再去学python的话,开始会觉的不太 习惯. 怎么看怎么都觉的别扭,也有一些朋友因为这个特别的格式与python失 ...

  9. EasyUI + EF + MVC4 后台截图

    到目前完成的页面截图,完成了增删改查几项功能的技术测试,在解决几个小问题,就重新设计结构开始一个完整的后台开发,坚持用博客和云笔记记录开发过程.

  10. ASIHTTPRequest的使用(转)

    转载自:http://fushengfei.iteye.com/blog/1147112 博客分类: IOS   原文地址:http://wiki.magiche.net/pages/viewpage ...