PAT1020. Delete At Most Two Characters (35)
Given a string which contains only lower case English letters, how many different strings you can get after deleting AT MOST TWO characters in it?
Input Specification:
Each input file contains one test case, which gives the string whose length is in [3, 106].
Output Specification:
Print in a line the number of different strings you can get after deleting at most 2 characters.
Sample Input:
ababcc
Sample Output:
15
Hint:
Deleting 0 character gets “ababcc”.
Deleting 1 character gets “babcc”, “aabcc”, “abbcc”, “abacc” and “ababc”.
Deleting 2 character gets “abcc”, “bbcc”, “bacc”, “babc”, “aacc”, “aabc”, “abbc”, “abac” and “abab”.
题意:给你一个字符串,问至多删除2个字符的情况下,可能造成几种不同的子串。
分析:我的思路是首先确定两个相同的字符的最远影响距离,我们不妨写写看。
abcc这种相邻的显然会有影响;abcdc这种呢,删除dc的cd得到的都是abc这个子串,所以有重复;abcdec呢?没错!这两个c并不能做到相互影响了,所以相同字符的最大影响区间为2。我知道这时候你肯定有思路了,我们当然只需要维护这三种状态了(区间2内没有相同字符,区间1内有相同字符,区间2内有相同字符),找一找规律便可以得到状态转移方程了。
思路:找状态转移方程的时候必定是先找到最朴素的,也就是情况最简单的(没有字符是相同的),那么我们就可以根据当前这个字符是否被删减而做出判断,即不删或删当前。
dp[i][j]=dp[i−1][j−1]+dp[i−1][j]
d
p
[
i
]
[
j
]
=
d
p
[
i
−
1
]
[
j
−
1
]
+
d
p
[
i
−
1
]
[
j
]
其余两种情况特判,当然
dp[i][j−1]
d
p
[
i
]
[
j
−
1
]
这一行的值需要提前预处理得到,也就是删一个字符,想必根据上面的思路找到最大影响区间也就能解决了哟!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
#define Irish_Moonshine main
const int maxn = 1e6 + 10;
char str[maxn];
ll dp[maxn][3];
int Irish_Moonshine()
{
scanf("%s", str);
int len = strlen(str);
for (int i = 0; i <= len; i++) dp[i][0] = 1;
dp[0][1] = 1;
for (int i = 1; i < len; i++){
dp[i][1] = dp[i - 1][0] + dp[i - 1][1];
if (str[i] == str[i - 1]) dp[i][1]--;
}
dp[1][2] = 1;
for (int i = 2; i < len; i++){
dp[i][2] = dp[i - 1][1] + dp[i - 1][2];
if (str[i] == str[i - 1]) dp[i][2] -= dp[i - 2][1];
else if (str[i] == str[i - 2]) dp[i][2]--;
}
ll ans = dp[len - 1][0] + dp[len - 1][1] + dp[len - 1][2];
printf("%lld\n", ans);
return 0;
}
//_CRT_SECURE_NO_WARNINGS
2018天梯赛L3-020. 至多删三个字符
给定一个全部由小写英文字母组成的字符串,允许你至多删掉其中 3 个字符,结果可能有多少种不同的字符串?
输入格式:
输入在一行中给出全部由小写英文字母组成的、长度在区间 [4, 106] 内的字符串。
输出格式:
在一行中输出至多删掉其中 3 个字符后不同字符串的个数。
输入样例:
ababcc
输出样例:
25
提示:
删掉 0 个字符得到 “ababcc”。
删掉 1 个字符得到 “babcc”, “aabcc”, “abbcc”, “abacc” 和 “ababc”。
删掉 2 个字符得到 “abcc”, “bbcc”, “bacc”, “babc”, “aacc”, “aabc”, “abbc”, “abac” 和 “abab”。
删掉 3 个字符得到 “abc”, “bcc”, “acc”, “bbc”, “bac”, “bab”, “aac”, “aab”, “abb” 和 “aba”。
分析:同样的,找到最大影响区间就可以了!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
#define Irish_Moonshine main
const int maxn = 1e6 + 10;
char str[maxn];
ll dp[maxn][4];
int Irish_Moonshine()
{
scanf("%s", str);
int len = strlen(str);
for (int i = 0; i <= len; i++) dp[i][0] = 1;
dp[0][1] = 1;
for (int i = 1; i < len; i++) {
dp[i][1] = dp[i - 1][0] + dp[i - 1][1];
if (str[i] == str[i - 1]) dp[i][1]--;
}
dp[1][2] = 1;
for (int i = 2; i < len; i++) {
dp[i][2] = dp[i - 1][1] + dp[i - 1][2];
if (str[i] == str[i - 1]) dp[i][2] -= dp[i - 2][1];
else if (str[i] == str[i - 2]) dp[i][2]--;
}
dp[2][3] = 1;
for (int i = 3; i < len; i++) {
dp[i][3] = dp[i - 1][3] + dp[i - 1][2];
if (str[i] == str[i - 1]) dp[i][3] -= dp[i - 2][2];
else if (str[i] == str[i - 2]) dp[i][3] -= dp[i - 3][1];
else if (str[i] == str[i - 3]) dp[i][3]--;
}
ll ans = dp[len - 1][0] + dp[len - 1][1] + dp[len - 1][2] + dp[len - 1][3];
printf("%lld\n", ans);
return 0;
}
//_CRT_SECURE_NO_WARNINGS