C. Constanzes Machine

news/2025/3/9 11:40:53/文章来源:https://www.cnblogs.com/yxcblogs/p/18757565

https://codeforces.com/problemset/problem/1245/C

题意:给定一个字符串s,字符串中连续的"nn"和"uu"可能由m和w变换而来,但也可能不是。问有多少种字符串是合法的?字符串如果包含任意的m和n则不合法。

思路:动态规划,依次考虑第i个位置的字符,如果i和i-1的字符是nn或者uu,那么dp[i] += dp[i - 2];

总结:字符串下标从0开始,dp下标从1开始~~ 因为有了模板,取模都不用管了,真好用啊

#include <bits/stdc++.h>#define dbg std::cout << "----------------------------------------" << std::endl;constexpr const std::array<int, 4> dx  { -1, 1, 0, 0 };
constexpr const std::array<int, 4> dy  { 0, 0, -1, 1 };
constexpr const std::array<int, 8> dx2 { -1, -1, 0, 1, 1, 1, 0, -1 };
constexpr const std::array<int, 8> dy2 { 0, -1, -1, -1, 0, 1, 1, 1 };constexpr const int         INF      =  0x3f3f3f3f;         /*  1e9 */
constexpr const long long   INF_LL   =  0x3f3f3f3f3f3f3f3f; /* 4e18 */template<typename T, typename U>
inline typename std::common_type<T, U>::type gcd(T a, U b) {typename std::common_type<T, U>::type t;while (b != 0) {t = a % b;a = b;b = t;}return a;
}
inline long long lcm(long long a, long long b) { return a / ::gcd(a, b) * b; }template<typename T, typename U>
inline bool checkMax(T& a, const U& b) { return a < b ? a = b, true : false; }
template<typename T, typename U>
inline bool checkMin(T& a, const U& b) { return a > b ? a = b, true : false; }inline int ppc  (int x)           { return __builtin_popcount(x); }
inline int ctz  (int x)           { return __builtin_ctz(x); }
inline int ppcll(long long x)     { return __builtin_popcountll(x); }
inline int ctzll(long long x)     { return __builtin_ctzll(x); }// template<typename T, typename U>
// inline T extendEuclid(T a, U b, int &x, int &y) {
//     x = 1, y = 0;
//     int x1 = 0, y1 = 1, a1 = a, b1 = b;
//     while (b1) {
//         int q = a1 / b1;
//         std::tie(x, x1) = std::make_tuple(x1, x - q * x1);
//         std::tie(y, y1) = std::make_tuple(y1, y - q * y1);
//         std::tie(a1, b1) = std::make_tuple(b1, a1 - q * b1);
//     }
//     return a1;
// }/*
* ModInt(自动进行模运算的数据类型)
* 设计思想:参考自tourist
* 基于面向对象的编程思想,本方法尽可能多的隐藏了内部实现的细节,并且将必要的编程接口暴露在外部,并需要对这些接口进行直接的修改。
* 
*                             使用方法: 将MInt作为基本数据类型来使用即可,无需考虑模除法运算与运算溢出。
*                                      将本文底部mod值改为需要使用的模数即可。
*                             幂运算:幂运算基于ModInt类中的静态函数MInt::power(MInt a, type b);
*                             注意事项:类内求逆元使用的是拓展欧几里得,如有需要可自行改成费马小定理。
*
* gitHub(仓库地址): https://github.com/yxc-s/programming-template.git*///TODO:增加更多的运算符重载
template<typename T, typename U, typename V>
inline T fastPower(T a, U b, const V& mod) {assert(b >= 0);T res = 1;for (; b > 0; a = 1ll * a * a % mod, b >>= 1) {if (b & 1) {res = 1ll * res * a % mod;}}return res;
}template<typename T, typename U>
inline T fermatInverse(const T& a, const U& mod) { return fastPower(a, mod - 2, mod); }template <typename T, typename U>
inline T extendEuclidInverse(T a, U mod) {T x = 0, y = 1;while (a != 0) {T q = mod / a;mod -= q * a; std::swap(a, mod);x -= q * y; std::swap(x, y);}assert(mod == 1);return x;
}template<typename T>
class ModInt {
public:using Type = typename std::decay<decltype(T::value)>::type;ModInt(long long value = 0) : value_(normalize(value)) {}ModInt(const ModInt<T>& other) = default;ModInt(ModInt<T>&& other) : value_(other.value_) {}~ModInt() = default;ModInt& operator += (const ModInt& other) { return value_ = normalize(value_ + other.value_), *this; }ModInt& operator -= (const ModInt& other) { return value_ = normalize(value_ - other.value_), *this; }ModInt& operator ++ () { return normalize(++value_), *this; }ModInt& operator -- () { return normalize(--value_), *this; }ModInt  operator ++ (int) { ModInt res{ *this }; return *this += 1, res; }ModInt  operator -- (int) { ModInt res(*this); return *this -= 1, res; }template<typename U> ModInt& operator += (const U& other) { return *this += ModInt(other); }template<typename U> ModInt& operator -= (const U& other) { return *this -= ModInt(other); }ModInt& operator =(const ModInt& other) {if (this != &other) {value_ = other.value_;}return *this;}template<typename U, typename = std::enable_if_t<std::is_integral_v<U>>>ModInt& operator =(U x) {value_ = normalize(x);return *this;}template <typename U = T>typename std::enable_if<std::is_same<typename ModInt<U>::Type, int>::value, ModInt>::type& operator *= (const ModInt& other) {value_ = normalize(static_cast<long long>(value_) * static_cast<long long>(other.value_));return *this;}template <typename U = T>typename std::enable_if<std::is_same<typename ModInt<U>::Type, long long>::value, ModInt>::type& operator *= (const ModInt& other) {long long q = static_cast<long long>(static_cast<long double>(value_) * other.value_ / getModValue());value_ = normalize(value_ * other.value - q * getModValue());return *this;}ModInt& operator /= (const ModInt& other) {//return *this *= ModInt(fermatInverse(other.value_, getModValue()));/*Fermat Inverse requires a prime Mod value!!*/return *this *= ModInt(extendEuclidInverse(other.value_, getModValue()));}template<typename U>friend bool operator == (const ModInt<U>& lhs, const ModInt<U>& rhs);template <typename U>friend bool operator < (const ModInt<U>& lhs, const ModInt<U>& rhs);template<typename U>friend std::ostream& operator << (std::ostream& os, const ModInt<U>& number) {os << number.value_;return os;}template<typename U>friend std::istream& operator >> (std::istream& is, ModInt<U>& number) {typename std::common_type<typename ModInt<U>::Type, long long>::type value;is >> value;number.value_ = normalize(value);return is;}template<typename U>static Type normalize(const U& value) {Type res = static_cast<Type> (value % getModValue());res = (res < 0 ? res + getModValue() : res > getModValue() ? res - getModValue() : res);return res;}/* 获取原本元素值,或者转为整形 */Type operator () () const { return this->value_; }operator int() const noexcept{ return static_cast<int>(value_); }operator long long() const noexcept { return static_cast<long long> (value_); }template<typename U, typename V>static U power(U a, V b) {assert(b >= 0);U res = 1;for (; b > 0; a = a * a, b >>= 1) {if (b & 1) {res = res * a;}}return res;}ModInt power(long long b) const noexcept { return ModInt::power(*this, b); }private:Type value_;constexpr static Type getModValue() { return T::value; }};template <typename T>             bool operator == (const ModInt<T>& lhs, const ModInt<T>& rhs) { return lhs.value_ == rhs.value_; }
template <typename T, typename U> bool operator == (const ModInt<T>& lhs, U rhs) { return lhs == ModInt<T>(rhs); }
template <typename T, typename U> bool operator == (U lhs, const ModInt<T>& rhs) { return ModInt<T>(lhs) == rhs; }template <typename T>             bool operator != (const ModInt<T>& lhs, const ModInt<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator != (const ModInt<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator != (U lhs, const ModInt<T>& rhs) { return !(lhs == rhs); }template <typename T> ModInt<T>             operator + (const ModInt<T>& lhs, const ModInt<T>& rhs) { return ModInt<T>(lhs) += rhs; }
template <typename T, typename U> ModInt<T> operator + (const ModInt<T>& lhs, U rhs) { return ModInt<T>(lhs) += rhs; }
template <typename T, typename U> ModInt<T> operator + (U lhs, const ModInt<T>& rhs) { return ModInt<T>(lhs) += rhs; }template <typename T>             ModInt<T> operator - (const ModInt<T>& lhs, const ModInt<T>& rhs) { return ModInt<T>(lhs) -= rhs; }
template <typename T, typename U> ModInt<T> operator - (const ModInt<T>& lhs, U rhs) { return ModInt<T>(lhs) -= rhs; }
template <typename T, typename U> ModInt<T> operator - (U lhs, const ModInt<T>& rhs) { return ModInt<T>(lhs) -= rhs; }template <typename T>             ModInt<T> operator * (const ModInt<T>& lhs, const ModInt<T>& rhs) { return ModInt<T>(lhs) *= rhs; }
template <typename T, typename U> ModInt<T> operator * (const ModInt<T>& lhs, U rhs) { return ModInt<T>(lhs) *= rhs; }
template <typename T, typename U> ModInt<T> operator * (U lhs, const ModInt<T>& rhs) { return ModInt<T>(lhs) *= rhs; }template <typename T>             ModInt<T> operator / (const ModInt<T>& lhs, const ModInt<T>& rhs) { return ModInt<T>(lhs) /= rhs; }
template <typename T, typename U> ModInt<T> operator / (const ModInt<T>& lhs, U rhs) { return ModInt<T>(lhs) /= rhs; }
template <typename T, typename U> ModInt<T> operator / (U lhs, const ModInt<T>& rhs) { return ModInt<T>(lhs) /= rhs; }template <typename T> bool operator < (const ModInt<T>& lhs, const ModInt<T>& rhs) { return lhs.value_ < rhs.value_; }constexpr int mod = (int)(1e9 + 7);
using MInt = ModInt<std::integral_constant<std::decay<decltype(mod)>::type, mod>>;/**/
using namespace std;inline void preProcess() {}inline void solve(){string s;cin >> s;for (auto& x : s) {if (x == 'm' || x == 'w') {cout << 0;return;}}int n = (int)s.size();vector<MInt> dp(n + 1);dp[0] = dp[1] = 1;for (int i = 2; i <= n; ++i) {dp[i] = dp[i - 1];if (s[i - 1] == s[i - 2] && (s[i - 1] == 'u' || s[i - 1] == 'n')) {dp[i] += dp[i - 2];}}cout << dp[n];
}int main() {std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);int tc = 1;preProcess();if constexpr (0){cin >> tc;}while (tc--) {solve();}#ifdef LOCAL_DEBUGstd::cout << std::endl;
#endifreturn 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/895064.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CH9121替换注意事项

CH9121A 基于前版CH9121(无后缀字母)升级,引脚基本兼容,替换时需调整外围电路。升级内容:精简供电方式由3.3&1.8v双电源供电改为3.3v单电源供电;I/O 口支持3.3V、2.5V、1.8V 供电,兼容多种电压标准的单片机;串口波特率支持300bps~10Mbps提供LED 状态显示Link 和AC…

飞书登录流程

不积跬步,无以至千里;不积小流,无以成江海。

Web基础

基础知识和工具 基础知识X-Forwarded-For:非常弱智的题才会有。工具GitHack:用来恢复.git文件 dirsearch:很强的crawl fastcoll:md5碰撞 AntSword:一句话木马GET连接PHP特性 精度 单精度23位尾数=\(\log_{10}2*23≈7\)位 双精度52位尾数=\(\log_{10}2*52≈16\)位 md50e开头…

35.2K star!双链笔记+知识图谱+本地优先,这款开源知识管理神器绝了!

一款融合「双链笔记+知识图谱+本地优先」理念的开源知识管理工具,支持Markdown/Org-mode双格式,打造你的第二大脑!🔥 一款融合「双链笔记+知识图谱+本地优先」理念的开源知识管理工具,支持Markdown/Org-mode双格式,打造你的第二大脑!项目介绍"Logseq 是一个注重隐…

作业2 - 个人项目

Info Detail学号 3123004432仓库链接 传送门如何使用? 使用 mvn exec:java(开发阶段) mvn exec:java \ -Dexec.mainClass="com.article.App" \ -Dexec.args="原文件路径 抄袭文件路径 输出路径"使用可执行 JAR(部署阶段) # 生成 JARmvn clean packa…

[I.1]个人作业:阅读和提问

[I.1] 个人作业:阅读和提问 ——coder0xe项目 内容这个作业属于哪个课程 2025春季软件工程(罗杰、任健)这个作业的要求在哪里 [I.1]个人作业:阅读和提问我在这个课程的目标是 在PSP中精进个人代码技术,在TSP中提高团队合作凝聚力这个作业在哪个具体方面帮助我实现目标 阅读教…

python3.4加装网络库

python3.4,旧东西加库,有点难,找不对版本。首先是requests,询问豆包requests-2.21.0可用,然后又是urllib,这个装错了,要1.25以下才行,在这里耽搁了很长时间。chardet-3.0.4,这个版本可用,chardet-3.0.2报错。certifi-2019.6.16一次成功,还有个idna27,用pip下载

进销存系统与WMS系统的区别是什么?一篇文章带你彻底搞懂!

说到 进销存系统 和 WMS(仓储管理系统),很多人都觉得这俩东西差不多,反正都是管库存的。 但真要选系统,或者用起来,就会发现完全不是一回事! 今天咱们就聊一聊,进销存和WMS到底有什么区别?在功能上有哪些不同?以及哪个更适合你?话不多说,直接上干货! 一、进销存和…

储油自动化革命,网关PROFINET与MODBUS网桥的无缝融合,锦上添花

储油行业作为能源供应链的关键环节,其自动化和监控系统的可靠性和效率至关重要。随着工业4.0的推进,储油设施越来越多地采用先进的自动化技术以提高安全性、降低成本并优化运营。本案例探讨了如何通过使用稳联技术PROFINET转MODBUS模块网关网桥,实现储油罐区的高效监控和控制…

第一个后端app

下面这一段代码直接沾到Pycharm里面去运行的话是不能运行成功的,这是因为我们不知道执行什么,应该加上我们要执行的东西,这样子代码就知道执行什么东西了(在这里就是执行我们定义的方法hello_world),如下有了app.run()之后就知道运行什么了 我们在命令行中输入下面的命令…

P5018 [NOIP 2018 普及组] 对称二叉树

链接 https://www.luogu.com.cn/problem/P5018 思路简单的递归,极致的享受。 就是判断左子树的右子树和右子树的左子树是否一样,用递归就行,加上点剪枝。 我感觉复杂度是N^2, 评论区给出的证明说是nlogn,看不懂,贴在下面。 但是能过就行代码 #include<bits/stdc++.h&g…