当前位置:网站首页>AT4379 [AGC027E] ABBreviate
AT4379 [AGC027E] ABBreviate
2022-07-22 04:24:00 【心怀凉月】
AT4379 [AGC027E] ABBreviate
分别记 a a a、 b b b 为 1 1 1、 2 2 2。
则操作 s i s j s_i \ s_j si sj 即合并为 ( s i + s j ) m o d 3 (s_i+s_j) \bmod 3 (si+sj)mod3。
值为 0 0 0 表示无法合并成一个;否则可以。
考虑通过若干次操作得到的一个字符串 t t t。
那么将全串从前往后贪心尽量取最短的段合并并与 t t t 匹配,最后剩下的串一定模 3 3 3 余 0 0 0 或是空串。
那么贪心从前往后匹配即可,具体见代码。
时间复杂度 O ( n ) \mathcal O(n) O(n)。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ha putchar(' ')
#define he putchar('\n')
inline int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * f;
}
inline void write(int x)
{
if(x < 0)
{
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + 48);
}
const int mod = 1e9 + 7;
int n, f[100005], nxt[100005][3], s[100005], ans;
char c[100005];
signed main()
{
scanf("%s", c + 1);
n = strlen(c + 1);
bool flg = 0;
for(int i = 1; i < n; ++i)
if(c[i] == c[i + 1]) {
flg = 1; break;}
if(!flg) {
return puts("1"), 0;}
for(int i = 1; i <= n; ++i) s[i] = (s[i - 1] + (c[i] == 'a' ? 1 : 2)) % 3;
for(int i = 0; i < 3; ++i) nxt[n][i] = n + 1;
for(int i = n; i >= 1; --i)
{
for(int j = 0; j < 3; ++j) nxt[i - 1][j] = nxt[i][j];
nxt[i - 1][s[i]] = i;
}
f[0] = 1;
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < 3; ++j)
if(s[i] != j) f[nxt[i][j]] = (f[nxt[i][j]] + f[i]) % mod;
if(s[i + 1] == s[n]) ans = (ans + f[i + 1]) % mod;
}
write(ans), he;
return 0;
}
边栏推荐
- Session共享问题
- Glide source code analysis
- How can the easycvr platform access special devices without authentication?
- 6-12漏洞利用-枚举smtp用户名
- Elephant Swap的LaaS方案迅速崛起,构建全新DeFi2.0协议
- 【解决方案】解决paddlepaddle运行强化学习代码时TypeError: fc() got an unexpected keyword argument ‘is_test‘的错误
- JVM memory model: PC program counters
- C # upload files to shared folders
- 字符编码问题
- 学生如何提高专业英文阅读能力丨传道授业
猜你喜欢
AlterNET脚本编写,用户界面设计功能
分布式调度框架Elastic-Job
Data structure in redis (2): jump table
How can the easycvr platform access special devices without authentication?
嵌入式IDE原理 OpenOCD介绍 以及stlink如何连接stm32板子
Vulkan official example interpretation subchannel
Leetcode 234. 回文链表
string的模拟实现
【Leetcode周赛--哈希表数对】6164.数位和相等数对的最大和
Repair the problem of adding device groups and editing exceptions on easycvr platform
随机推荐
[ssm]ssm integration ② (development of functional modules)
Leetcode 172. zero after factorial
交換機與路由器技術:標准ACL、擴展ACL和命名ACL
进程和线程面试问题
The two supply chain centers of HEMA launched the "background" of innovative research and development of multi format commodities
When the easycvr platform cascades, there is an error prompt. What is the reason why the port is unreachable?
JVM memory model: class loading process
MP query criteria
Simplified writing of not like in MySQL
Kalman filter program of POTU PLC signal processing series
Command line code for server and local data transmission
Repair the problem of adding device groups and editing exceptions on easycvr platform
分布式调度框架Elastic-Job
mysql 数据库列拼接查询写法
Lesson 3 shell syntax
6-12 exploit - enumerate SMTP user names
【Leetcode周赛--哈希表数对】6164.数位和相等数对的最大和
Application level questions of computer network
The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
MySQL查询计划key_len如何计算