当前位置:网站首页>ABC#237 C
ABC#237 C
2022-07-05 08:38:00 【Stay--hungry】
C
Original link
The main idea of the topic : Given a string str, Determine whether you can add ’a’, Make the string become palindrome string .
The basic idea : take str Head and tail ’a’ Remove , See if the remaining strings are palindromes . If the remaining substring is not a palindrome string , Then the conclusion is No .
Here's the thing to watch out for : Because you can only add ’a’, So if str The head of the ’a’ The number of is larger than that of the tail ’a’ There are many of them , Then the conclusion is whether .
skill : The judgment of palindrome string
bool isPalindrome(string str)
{
string str2 = str;
reverse(str.begin(), str.end());
if (str == str2) return true;
else return false;
}
Code 1: Double pointer
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1000010;
char s[N];
int main()
{
cin >> s;
bool flag = 1;
int i = 0, j = strlen(s) - 1, cnt1 = 0, cnt2 = 0;
while (s[i] == 'a') i ++, cnt1 ++;
while (s[j] == 'a') j --, cnt2 ++;
if (cnt1 > cnt2) flag = 0;
while (i < j)
{
if (i >= j) break;
else if (s[i] != s[j])
{
flag = 0;
break;
}
else i ++, j --;
}
if (flag) cout << "Yes";
else cout << "No";
return 0;
}
Code 2:string
#include <iostream>
#include <algorithm>
using namespace std;
#define all(x) (x).begin(), (x).end()
int main()
{
string str, str2;
int cnt1 = 0, cnt2 = 0;
cin >> str;
while (str.size() && str.back() == 'a') str.pop_back(), cnt1 ++; // Remove the tail ‘a’
reverse(all(str));
while (str.size() && str.back() == 'a') str.pop_back(), cnt2 ++; // Take off the head ‘a’
str2 = str;
reverse(all(str));
if (str == str2 && cnt2 <= cnt1) cout << "Yes";
else cout << "No";
}
边栏推荐
- 【日常训练】1200. 最小绝对差
- Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
- STM32---ADC
- STM32---IIC
- Illustration of eight classic pointer written test questions
- STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
- Search data in geo database
- Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
- [nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
- Keil use details -- magic wand
猜你喜欢

STM32 lights up the 1.8-inch screen under Arduino IDE

Guess riddles (8)

Count the number of inputs (C language)

Guess riddles (5)

An enterprise information integration system

猜谜语啦(11)

Agile project management of project management

猜谜语啦(10)

MySQL之MHA高可用集群

Detailed summary of FIO test hard disk performance parameters and examples (with source code)
随机推荐
如何写Cover Letter?
319. Bulb switch
One question per day - replace spaces
Lori remote control LEGO motor
EA introduction notes
Five design details of linear regulator
Sword finger offer 05 Replace spaces
GEO数据库中搜索数据
Some pitfalls of win10 network sharing
Example 008: 99 multiplication table
【三层架构】
猜谜语啦(6)
猜谜语啦(10)
轮子1:QCustomPlot初始化模板
An enterprise information integration system
【日常训练】1200. 最小绝对差
Business modeling | process of software model
Warning: retrying occurs during PIP installation
实例006:斐波那契数列
Various types of questions judged by prime numbers within 100 (C language)