orber
1
#include <bits/stdc++.h>
using namespace std;
void lapindrome(string p){
string temp1,temp2;
bool check=false;
unordered_map<char,int> s1;
unordered_map<char,int> s2;
for (int i = 0; i < p.size()/2; ++i)
{
s1[i]++;
temp1.push_back(p[i]);
}
for (int i = (p.size()/2)+1; i < p.size(); ++i)
{
s2[i]++;
temp2.push_back(p[i]);
}
for (auto i : temp1)
{
if (s1[i]==s2[i] && temp1[i]==temp2[i])
{
check = true;
}
}
if (check)
{
cout<<"YES\n";
}
else{
cout<<"NO\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t;
cin >>t;
while(t--){
string s;
cin>>s;
lapindrome(s);
}
return 0;
}
I am getting wrong answer but it passing known test cases.
https://www.codechef.com/viewsolution/49376831
ssjgz
2
Out-of-bounds access with sample testcase:
[simon@simon-laptop][13:08:06]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling orber-LAPIN.cpp
+ g++ -std=c++14 orber-LAPIN.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv -fno-sanitize-recover
orber-LAPIN.cpp: In function ‘void lapindrome(std::__cxx11::string)’:
orber-LAPIN.cpp:8:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < p.size()/2; ++i)
~~^~~~~~~~~~~~
orber-LAPIN.cpp:10:13: warning: conversion to ‘std::__cxx1998::unordered_map<char, int, std::hash<char>, std::equal_to<char>, std::allocator<std::pair<const char, int> > >::key_type {aka char}’ from ‘int’ may alter its value [-Wconversion]
s1[i]++;
^
orber-LAPIN.cpp:13:30: warning: conversion to ‘int’ from ‘std::__cxx11::basic_string<char>::size_type {aka long unsigned int}’ may alter its value [-Wconversion]
for (int i = (p.size()/2)+1; i < p.size(); ++i)
~~~~~~~~~~~~^~
orber-LAPIN.cpp:13:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = (p.size()/2)+1; i < p.size(); ++i)
~~^~~~~~~~~~
orber-LAPIN.cpp:15:13: warning: conversion to ‘std::__cxx1998::unordered_map<char, int, std::hash<char>, std::equal_to<char>, std::allocator<std::pair<const char, int> > >::key_type {aka char}’ from ‘int’ may alter its value [-Wconversion]
s2[i]++;
^
+ set +x
Successful
[simon@simon-laptop][13:08:12]
[~/devel/hackerrank/otherpeoples]>echo "6
gaga
abcde
rotor
xyzxy
abbaab
ababc" | ./a.out
/usr/include/c++/7/bits/basic_string.h:1057: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]: Assertion '__pos <= size()' failed.
Aborted (core dumped)
According to gdb, it is this line:
if (s1[i]==s2[i] && temp1[i]==temp2[i])
temp1[i]
- i
is a char
, remember, and temp1[...]
expects a zero-relative index into a string!