当前位置:网站首页>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";
}
边栏推荐
- 關於線性穩壓器的五個設計細節
- Digital analog 2: integer programming
- 实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
- Keil use details -- magic wand
- Affected tree (tree DP)
- Arduino+a4988 control stepper motor
- Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
- 287. 寻找重复数-快慢指针
- 【NOI模拟赛】汁树(树形DP)
- Detailed summary of FIO test hard disk performance parameters and examples (with source code)
猜你喜欢
实例010:给人看的时间
Business modeling of software model | object modeling
Business modeling | process of software model
EA introduction notes
Example 006: Fibonacci series
L298N module use
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
猜谜语啦(5)
Business modeling of software model | stakeholders
随机推荐
696. Count binary substring
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
实例006:斐波那契数列
猜谜语啦(7)
Buildroot system for making raspberry pie cm3
[three tier architecture]
Array integration initialization (C language)
STM32 --- GPIO configuration & GPIO related library functions
轮子1:QCustomPlot初始化模板
Speech recognition learning summary
STM32 --- serial port communication
Guess riddles (3)
猜谜语啦(2)
2020-05-21
Several problems to be considered and solved in the design of multi tenant architecture
关于线性稳压器的五个设计细节
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
MySQL之MHA高可用集群
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?