由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - DP与Greedy的题
相关主题
Given a document, how to find pairs of words with same charactors but different order.贡献一道G家的面试题
问个GG面经里的题问G家一道电面题
从一道简单计数排序题看test cases的枚举周末上道小题吧anagram的
Leetcode OJ的编译器是?准备转回Java了
问两道数字题leetcode上最搞笑的是这题
amazon面试题目讨论贴2晕!java没有pass parameters by reference啊
问一个G家面试题LeetCode: Word Ladder
上一道题求解这个动态规划题
相关话题的讨论汇总
话题: letters话题: string话题: dp话题: forbidden话题: greedy
进入JobHunting版参与讨论
1 (共1页)
p*****2
发帖数: 21240
1
前两天有人问DP和Greedy的区别,今天做了一道题不错。我先用DP解,当时感觉有点别
扭,因为觉得好像有条件没有用上,结果超时。后来才意识到这题是Greedy. 想感觉一
下DP和Greedy的可以练练。
Sergey attends lessons of the N-ish language. Each lesson he receives a
hometask. This time the task is to translate some sentence to the N-ish
language. Sentences of the N-ish language can be represented as strings
consisting of lowercase Latin letters without spaces or punctuation marks.
Sergey totally forgot about the task until half an hour before the next
lesson and hastily scribbled something down. But then he recollected that in
the last lesson he learned the grammar of N-ish. The spelling rules state
that N-ish contains some "forbidden" pairs of letters: such letters can
never occur in a sentence next to each other. Also, the order of the letters
doesn't matter (for example, if the pair of letters "ab" is forbidden, then
any occurrences of substrings "ab" and "ba" are also forbidden). Also, each
pair has different letters and each letter occurs in no more than one
forbidden pair.
Now Sergey wants to correct his sentence so that it doesn't contain any "
forbidden" pairs of letters that stand next to each other. However, he is
running out of time, so he decided to simply cross out some letters from the
sentence. What smallest number of letters will he have to cross out? When a
letter is crossed out, it is "removed" so that the letters to its left and
right (if they existed), become neighboring. For example, if we cross out
the first letter from the string "aba", we get the string "ba", and if we
cross out the second letter, we get "aa".
Input
The first line contains a non-empty string s, consisting of lowercase Latin
letters — that's the initial sentence in N-ish, written by Sergey. The
length of string s doesn't exceed 105.
The next line contains integer k (0 ≤ k ≤ 13) —
the number of forbidden pairs of letters.
Next k lines contain descriptions of forbidden pairs of letters. Each line
contains exactly two different lowercase Latin letters without separators
that represent the forbidden pairs. It is guaranteed that each letter is
included in no more than one pair.
Output
Print the single number — the smallest number of letters that need to be
removed to get a string without any forbidden pairs of neighboring letters.
Please note that the answer always exists as it is always possible to remove
all letters.
Sample test(s)
input
ababa
1
ab
output
2
input
codeforces
2
do
cs
output
1
r*****k
发帖数: 1281
2
背景是算法高手啊。

★ 发自iPhone App: ChineseWeb - 中文网站浏览器

【在 p*****2 的大作中提到】
: 前两天有人问DP和Greedy的区别,今天做了一道题不错。我先用DP解,当时感觉有点别
: 扭,因为觉得好像有条件没有用上,结果超时。后来才意识到这题是Greedy. 想感觉一
: 下DP和Greedy的可以练练。
: Sergey attends lessons of the N-ish language. Each lesson he receives a
: hometask. This time the task is to translate some sentence to the N-ish
: language. Sentences of the N-ish language can be represented as strings
: consisting of lowercase Latin letters without spaces or punctuation marks.
: Sergey totally forgot about the task until half an hour before the next
: lesson and hastily scribbled something down. But then he recollected that in
: the last lesson he learned the grammar of N-ish. The spelling rules state

l*****a
发帖数: 14598
3
都不懂的先顶后看

in

【在 p*****2 的大作中提到】
: 前两天有人问DP和Greedy的区别,今天做了一道题不错。我先用DP解,当时感觉有点别
: 扭,因为觉得好像有条件没有用上,结果超时。后来才意识到这题是Greedy. 想感觉一
: 下DP和Greedy的可以练练。
: Sergey attends lessons of the N-ish language. Each lesson he receives a
: hometask. This time the task is to translate some sentence to the N-ish
: language. Sentences of the N-ish language can be represented as strings
: consisting of lowercase Latin letters without spaces or punctuation marks.
: Sergey totally forgot about the task until half an hour before the next
: lesson and hastily scribbled something down. But then he recollected that in
: the last lesson he learned the grammar of N-ish. The spelling rules state

p*****2
发帖数: 21240
4

大牛又来装嫩了?

【在 l*****a 的大作中提到】
: 都不懂的先顶后看
:
: in

h**6
发帖数: 4160
5
1.each pair has different letters
2.each letter occurs in no more than one forbidden pair.
这两个条件很重要。
B*******1
发帖数: 2454
6
弱问,dp咋做的?
b***u
发帖数: 12010
7
greedy难在不知道greedy可以。用dp绕半天浪费时间。greedy关键要能证明能贪

in

【在 p*****2 的大作中提到】
: 前两天有人问DP和Greedy的区别,今天做了一道题不错。我先用DP解,当时感觉有点别
: 扭,因为觉得好像有条件没有用上,结果超时。后来才意识到这题是Greedy. 想感觉一
: 下DP和Greedy的可以练练。
: Sergey attends lessons of the N-ish language. Each lesson he receives a
: hometask. This time the task is to translate some sentence to the N-ish
: language. Sentences of the N-ish language can be represented as strings
: consisting of lowercase Latin letters without spaces or punctuation marks.
: Sergey totally forgot about the task until half an hour before the next
: lesson and hastily scribbled something down. But then he recollected that in
: the last lesson he learned the grammar of N-ish. The spelling rules state

p*****2
发帖数: 21240
8

是。因为做过很多类似的DP题,上来就往DP那里想了。做的时候才发觉DP帮助不明显。
但是测试用例也都过了,就提交了。这题证明greedy可以也得花一些时间。不是很直观
。如果只是有思路的话,还是会在greedy和dp只见纠缠。感觉比赛的时候很难掌握呀。
另外,上一下greedy的代码。
public class test2 {
static String s;
static int k;
static HashSet pairs = new HashSet();
static boolean isForbidden(char[] arr) {
Arrays.sort(arr);
return pairs.contains(new String(arr));
}
static int Play(String str) {
int count = 0;
int count1 = 0;
int count2 = 0;
int j = 0;
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) == str.charAt(i + 1))
count1++;
else {
if (isForbidden(str.substring(i, i + 2).toCharArray())) {
for (j = i; j < str.length(); j++) {
if (str.charAt(j) == str.charAt(i))
count1++;
else if (str.charAt(j) == str.charAt(i + 1))
count2++;
else {
break;
}
}
} else {
count1 = 0;
}
}
if (j != 0) {
count += Math.min(count1, count2);
i = j - 1;
count1 = 0;
count2 = 0;
j = 0;
}
}
return count;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
s = in.next();
k = in.nextInt();
if (k == 0) {
System.out.println(0);
return;
}
for (int i = 0; i < k; i++) {
char[] arr = in.next().toCharArray();
Arrays.sort(arr);
pairs.add(new String(arr));
}
System.out.println(Play(s));
}
}

【在 b***u 的大作中提到】
: greedy难在不知道greedy可以。用dp绕半天浪费时间。greedy关键要能证明能贪
:
: in

i***h
发帖数: 12655
9
有第二个条件的话, 岂不是扫一遍就可以了?
如果要去掉 ab 的话,
只要解决 aaab 和 abbb 的情形就可以了

【在 h**6 的大作中提到】
: 1.each pair has different letters
: 2.each letter occurs in no more than one forbidden pair.
: 这两个条件很重要。

l********k
发帖数: 613
10
这个解法不对吧,如果ab是一个forbidden pair, 那么遇到aabbbaa这种情况,应该划
掉bbb,而不是aa。

【在 p*****2 的大作中提到】
:
: 是。因为做过很多类似的DP题,上来就往DP那里想了。做的时候才发觉DP帮助不明显。
: 但是测试用例也都过了,就提交了。这题证明greedy可以也得花一些时间。不是很直观
: 。如果只是有思路的话,还是会在greedy和dp只见纠缠。感觉比赛的时候很难掌握呀。
: 另外,上一下greedy的代码。
: public class test2 {
: static String s;
: static int k;
: static HashSet pairs = new HashSet();
: static boolean isForbidden(char[] arr) {

i***h
发帖数: 12655
11
对于ab, 找出任何一个 (a*b*)* 的子串,
如果子串里a的数目大于b, 删b, 反之删a

【在 l********k 的大作中提到】
: 这个解法不对吧,如果ab是一个forbidden pair, 那么遇到aabbbaa这种情况,应该划
: 掉bbb,而不是aa。

p*****2
发帖数: 21240
12

我的代码会划掉b的。你再仔细看看。

【在 l********k 的大作中提到】
: 这个解法不对吧,如果ab是一个forbidden pair, 那么遇到aabbbaa这种情况,应该划
: 掉bbb,而不是aa。

1 (共1页)
进入JobHunting版参与讨论
相关主题
求解这个动态规划题问两道数字题
杯具!越改越差amazon面试题目讨论贴2
Isomorphic Strings 的单Hashmap解法问一个G家面试题
leetcode ValidNumber问题的代码,供参考上一道题
Given a document, how to find pairs of words with same charactors but different order.贡献一道G家的面试题
问个GG面经里的题问G家一道电面题
从一道简单计数排序题看test cases的枚举周末上道小题吧anagram的
Leetcode OJ的编译器是?准备转回Java了
相关话题的讨论汇总
话题: letters话题: string话题: dp话题: forbidden话题: greedy