当前位置:网站首页>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";
}
边栏推荐
- Cinq détails de conception du régulateur de tension linéaire
- STM32 single chip microcomputer -- volatile keyword
- Classification of plastic surgery: short in long long long
- Business modeling | process of software model
- Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
- Stm32--- systick timer
- leetcode - 445. Add two numbers II
- STM32 --- serial port communication
- Arduino operation stm32
- C language data type replacement
猜你喜欢
随机推荐
MySQL MHA high availability cluster
Arduino burning program and Arduino burning bootloader
UE像素流,来颗“减肥药”吧!
How can fresh students write resumes to attract HR and interviewers
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
轮子1:QCustomPlot初始化模板
STM32 --- serial port communication
STM32---IIC
Example 009: pause output for one second
Buildroot system for making raspberry pie cm3
Sword finger offer 06 Print linked list from end to end
STM32 lights up the 1.8-inch screen under Arduino IDE
Count the number of inputs (C language)
实例010:给人看的时间
Daily question - input a date and output the day of the year
Example 006: Fibonacci series
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Shell script realizes the reading of serial port and the parsing of message
【NOI模拟赛】汁树(树形DP)
Cmder of win artifact