-1

this is a simple program to check for a substring which is a palindrome. it works fine for string of length 1000 but gives TLE error on SPOJ for a length of 100000. how shall i optimize this code. saving all the substrings will not work for such large inputs. the time limit is 1 sec so we can do at most 10^6-10^7 iterations. is there any other way i can do it.

#include<bits/stdc++.h>

int main()
{
    int t;
    std::cin>>t;
    if(t<1||t>10)
        return 0;
    while(t--)
    {
        std::string s;
        std::cin>>s;
        //std::cout<<s.substr(0,1);
        //std::vector<std::string>s1;
        int n=s.length();
        if(n<1||n>100000)
            return 0;
            int len,mid,k=0,i=0;
        for(i=0;i<n-1;i++)
        {
            for(int j=2;j<=n-i;j++)
            {
                std::string ss=s.substr(i,j);
                //s1.push_back(ss);
            len=ss.length();
            mid=len/2;
            while(k<=mid&&(len-1-k)>=mid&&len>1)
            {
                if(ss[k]!=ss[len-1-k])
                    break;
                k++;
            }
            if(k>mid||(len-1-k)<mid)
            {
                std::cout<<"YES"<<std::endl;
                break;
            }
            }
            if(k>mid||(len-1-k)<mid)
                break;
        }

        if(i==n-1)
            std::cout<<"NO"<<std::endl;
            //for(i=0;i<m;i++)
              //  std::cout<<s1[i]<<std::endl
    }
    return 0;
}
Sohit Gore
  • 438
  • 1
  • 5
  • 13
  • How about you write your algorithm down in English? If you can describe it in English (or your native language) you'll find it much easier to see why it's going slowly. – UKMonkey Aug 25 '17 at 11:21
  • 2
    Write a palindrome-checking function. Apply it to each substring without saving them. – molbdnilo Aug 25 '17 at 11:22
  • 1
    imho asking to review code with single letter variables names only is... not nice. Anyhow for review there is https://codereview.stackexchange.com/, though I wouldnt expect that this will be well received there either – 463035818_is_not_an_ai Aug 25 '17 at 11:23
  • @tobi303 The OP would need to reword this a bit. Just "This is my code. It achieves X by doing Y & Z. How can I make it faster?". As for the code being terrible, that's really not a problem for us. As long as it works as intended, we're happy to review it. – Kaz Aug 25 '17 at 11:28

2 Answers2

0

I'm not completely sure what your function is trying to accomplish... are you finding t palindromic substrings?

To save on memory, rather than store every substring in a vector and then iterating over the vector to check for palindromes, why not just check if the substring is a palindrome as you generate them?

std::string ss = s.substr(i,j);
// s1.push_back(ss);  // Don't store the substrings
if (palindromic(ss)) {
    std::cout << "YES" << std::endl;
    break;
}

This saves some time as most cases, as you no longer always generate every possible substring. However, it is not guaranteed to be much faster in the worst case.

AZhu
  • 28
  • 1
  • 7
0

Your assumption that saving all sub-strings in another vector and checking them later with same O(N^2) approach will not help you to reduce time complexity of your algorithm. Instead, it will increases your memory complexity too. Holding all possible sub-strings in another vector will take lots of memory.

Since the size of string could be maximum of 10^5. To check if there exist any palindromic sub-string should be done either in O(NlogN) or O(N) time complexity to pass within time limit. For this, I suggest you two algorithms:
1.) Suffix array : Link here
2.) Manacher’s Algorithm: Link here

BishalG
  • 1,414
  • 13
  • 24
  • number of substrings of a string are (n^2+n)/2 hence the algorithm would take O(n^2) time how do we suppose to do it in O(nlogn) or O(n). anyways thanks for the help – Sohit Gore Aug 26 '17 at 16:59