当前位置:网站首页>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";
}
边栏推荐
- Several problems to be considered and solved in the design of multi tenant architecture
- 暑假第一周
- Pytorch entry record
- Guess riddles (7)
- Tips 1: Web video playback code
- MATLAB skills (28) Fuzzy Comprehensive Evaluation
- go依赖注入--google开源库wire
- 287. 寻找重复数-快慢指针
- Shell script
- Business modeling of software model | overview
猜你喜欢
随机推荐
Guess riddles (8)
实例009:暂停一秒输出
猜谜语啦(7)
Sword finger offer 05 Replace spaces
猜谜语啦(11)
GEO数据库中搜索数据
C语言标准函数scanf不安全的原因
UE pixel stream, come to a "diet pill"!
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
How apaas is applied in different organizational structures
Pytorch entry record
2022.7.4-----leetcode.1200
Reasons for the insecurity of C language standard function scanf
Stm32--- systick timer
STM32 --- configuration of external interrupt
How can fresh students write resumes to attract HR and interviewers
Digital analog 2: integer programming
Guess riddles (2)
Esp8266 interrupt configuration
【NOI模拟赛】汁树(树形DP)








