wannafly挑战赛28 b

给一个长度为 $n$ 包含字母 $msc$ 的字符串,问包含子序列 $mcc$ 和 $msc$ 的子串数量。
$(n le 10^5)$


题解

$msc$ 和 $msc$ 组合起来一共就 $8$ 中相对顺序,故暴力保存每一个字母的位置,暴力对每一个 $m$ 开头找合法的序列即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

using namespace std;
typedef long long ll;
const int maxn = 1e5+100;

int n, len;
char s[maxn];
int str[8][6]={
{0,0,1,2,2,2},
{0,0,2,1,2,2},
{0,0,2,2,1,2},
{0,1,0,2,2,2},
{0,1,2,0,2,2},
{0,2,0,1,2,2},
{0,2,0,2,1,2},
{0,2,2,0,1,2}
};

vector<int>pos[40];
ll ans;

int (int type, int p)
{
return *lower_bound(pos[type].begin(), pos[type].end(), p);
}

int main()
{
scanf("%d%s", &n, s+1);
pos[0].push_back(0);
for(int i=1;i<=n;i++)
{
if(s[i] == 'm') pos[0].push_back(i);
else if(s[i] == 's') pos[1].push_back(i);
else pos[2].push_back(i);
}
pos[0].push_back(n+1);
pos[1].push_back(n+1);
pos[2].push_back(n+1);

for(int i=1;i<pos[0].size()-2;i++)
{
int res=n+1;
for(int j=0;j<8;j++)
{
int now=pos[0][i];
for(int k=1;k<6 && now<=n;k++)
{

now = min(n+1, check(str[j][k], now+1));

}
res = min(res, now);

}
if(res == n+1) break;
ans = ans + 1LL * (pos[0][i]-pos[0][i-1]) * (n-res+1);

}
printf("%lldn", ans);
return 0;
}