当前位置:网站首页>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";
}
边栏推荐
- Some pitfalls of win10 network sharing
- Five design details of linear regulator
- Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
- 99 multiplication table (C language)
- STM32 outputs 1PPS with adjustable phase
- Various types of questions judged by prime numbers within 100 (C language)
- How to write cover letter?
- 319. 灯泡开关
- 287. Looking for repeats - fast and slow pointer
- 猜谜语啦(3)
猜你喜欢
随机推荐
Guess riddles (4)
99 multiplication table (C language)
关于线性稳压器的五个设计细节
MATLAB skills (28) Fuzzy Comprehensive Evaluation
Guess riddles (7)
U8g2 drawing
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
Apaas platform of TOP10 abroad
An enterprise information integration system
Daily question - input a date and output the day of the year
Run menu analysis
Guess riddles (6)
Illustration of eight classic pointer written test questions
leetcode - 445. 两数相加 II
Search data in geo database
实例010:给人看的时间
第十八章 使用工作队列管理器(一)
Keil use details -- magic wand
Digital analog 1: linear programming
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘