Codeforces 155 C. Hometask
2014-08-01 21:53
225 查看
DP....
C. Hometask
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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
output
input
output
Note
In the first sample you should remove two letters b.
In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[110000];
int n;
char xx[20][2];
int dp[110000][3],f[110000][3];
int cross[50][50];
int main()
{
str[0]=29;
scanf("%s",str+1);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",xx[i]);
cross[xx[i][0]-'a'][xx[i][1]-'a']=1;
cross[xx[i][1]-'a'][xx[i][0]-'a']=1;
}
dp[1][1]=1;dp[1][0]=0;f[1][1]=str[1]-'a';f[1][0]=26;
n=strlen(str)-1;
for(int i=2;i<=n;i++)
{
dp[i][1]=1; f[i][1]=str[i]-'a';
int mx=1000;
for(int j=i-1;j>=1&&j>=dp[i][1];j--)
{
mx--;
if(cross[str[i]-'a'][str[j]-'a']==0)
{
if(dp[i][1]<dp[j][1]+1)
{
dp[i][1]=dp[j][1]+1;
}
}
if(mx<=0) break;
}
if(cross[str[i]-'a'][f[i-1][1]]==0)/// i i-1 bu chong tu
{
if(dp[i][1]<dp[i-1][1]+1)
{
dp[i][1]=dp[i-1][1]+1;
f[i][1]=str[i]-'a';
}
}
if(dp[i-1][0]>dp[i][0])
{
dp[i][0]=dp[i-1][0];
f[i][0]=f[i-1][0];
}
if(dp[i-1][1]>dp[i][0])
{
dp[i][0]=dp[i-1][1];
f[i][0]=f[i-1][1];
}
}
cout<<n-max(dp
[0],dp
[1])<<endl;
return 0;
}
C. Hometask
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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
2do
cs
output
1
Note
In the first sample you should remove two letters b.
In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[110000];
int n;
char xx[20][2];
int dp[110000][3],f[110000][3];
int cross[50][50];
int main()
{
str[0]=29;
scanf("%s",str+1);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",xx[i]);
cross[xx[i][0]-'a'][xx[i][1]-'a']=1;
cross[xx[i][1]-'a'][xx[i][0]-'a']=1;
}
dp[1][1]=1;dp[1][0]=0;f[1][1]=str[1]-'a';f[1][0]=26;
n=strlen(str)-1;
for(int i=2;i<=n;i++)
{
dp[i][1]=1; f[i][1]=str[i]-'a';
int mx=1000;
for(int j=i-1;j>=1&&j>=dp[i][1];j--)
{
mx--;
if(cross[str[i]-'a'][str[j]-'a']==0)
{
if(dp[i][1]<dp[j][1]+1)
{
dp[i][1]=dp[j][1]+1;
}
}
if(mx<=0) break;
}
if(cross[str[i]-'a'][f[i-1][1]]==0)/// i i-1 bu chong tu
{
if(dp[i][1]<dp[i-1][1]+1)
{
dp[i][1]=dp[i-1][1]+1;
f[i][1]=str[i]-'a';
}
}
if(dp[i-1][0]>dp[i][0])
{
dp[i][0]=dp[i-1][0];
f[i][0]=f[i-1][0];
}
if(dp[i-1][1]>dp[i][0])
{
dp[i][0]=dp[i-1][1];
f[i][0]=f[i-1][1];
}
}
cout<<n-max(dp
[0],dp
[1])<<endl;
return 0;
}
相关文章推荐
- Codeforces 155C Hometask【思维+Dp】
- Codeforces 155 C.Double Profiles
- codeforces 214B - Hometask
- CodeForces 154A Hometask dp
- codeforces 155 Div2 B
- codeforces 154A Hometask 贪心 dp
- Hometask--codeForces 214B
- CodeForces 214B Hometask
- Codeforces 154A Hometask
- codeforces 214B Hometask
- Codeforces 214B Hometask
- 【CodeForces 155C Hometask】白濑肆×字符串+DP——果然是字符串处理什么的好讨厌啊尤其是换行符的处理看来不用CIN不行了呢DP的转移真心不会啊水到家了怎么办!【1.1%达成】
- Codeforces 155 D. Colliders
- codeforces 183A Headquarters
- CodeForces 189A Cut Ribbon
- codeforces 628C Bear and String Distance
- CodeForces 189A Cut Ribbon
- (双指针尺取) CodeForces - 372A Counting Kangaroos is Fun
- Codeforces 106 D. Treasure Island(前缀和预处理)
- Codeforces 8D Two Friends 三圆交集是否为非空集合 二分 旋转x轴