LeetCode: Valid Palindrome

Problem:
https://leetcode.com/problems/valid-palindrome/
class Solution {
public:
    bool isPalindrome(string s) {
        int n=s.length(),f=1;
 string temp="";
 for(int i=0;i<n;i++) 
 {
  if(isalpha(s[i])||isdigit(s[i]))
  temp+=tolower(s[i]);
 }
 int len=temp.length();
 for(int i=0;i<len/2;i++) 
 {
  if(temp[i]!=temp[len-i-1])
  {
   return false;
  }
 }
 return true;
    }
};

SPOJ: LASTDIG

Problem:
Just find the power of Last disit using Logarithmic exponentiation
Here is the C++ implementation
#include<stdio.h>
long long modexpo(long long a,long long b,long long n)
{
    long long d=1;
    while(b)
    {
        if(b%2)
            d=(d*a)%n;
        b>>=1;
        a=(a*a)%n;
    }
    return d;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long a,b;
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",modexpo(a,b,10));
    }
    return 0;
}

SPOJ: STPAR

Problem:
http://www.spoj.com/problems/FINDSR/
The problem is about re-ordering the given trucks in an increasing order by using a temporary street.We can solve it by using stack
Here is the C++ implementation
#include<cstdio>
#include<vector>
#include<stack>
#include<iostream>
using namespace std;
int main()
{
    long long n;
     cin>>n;
     while(n)
    {
        long long ans=1;
 
        long long a[n+10];
        for(long long i=1;i<=n;i++)
            cin>>a[i];
        stack<long long>st;
        long long strt=1;
        for(long long i=1;i<=n;i++)
        {
            if(a[i]==strt)
                strt++;
            else
            {
                if(st.empty())
                {
                      st.push(a[i]);
 
                }
 
                else if(!st.empty() && st.top()<a[i])
                {
 
                    ans=0;
                    break;
                }
                else
                    st.push(a[i]);
            }
 
            while(!st.empty() && st.top()==strt)
            {
                st.pop();
                strt++;
            }
        }
        if(!ans)
            printf("no\n");
        else if(strt-1==n && st.empty())
            printf("yes\n");
            cin>>n;
    }
}

SPOJ:FINDSR

Problem:
http://www.spoj.com/problems/FINDSR/
This problem can be solved using kmp prefix function
Below is the working code for above problem
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
void prefix(char *s,int n,int *pref)
{
 int x,y;
 int j=0,i;
 pref[0]=0;
 for(int i=1;i<n;i++)
 {
  while(j>0 && s[j]!=s[i])
  j=pref[j-1];
  if(s[i]==s[j]) 
  j++;
  pref[i]=j;
 }
 int ans=1;
 x=n-pref[n-1];
 if(n%x==0)
 ans=n/x;
 printf("%d\n",ans); 
}
int main()
{
 
 char pat[100005];
 scanf("%s",pat);
 while(strcmp(pat,"*")!=0)
 {
 int n=strlen(pat);
 int pref[n+1];
 prefix(pat,n,pref);
 scanf("%s",pat);
 }
} 

SPOJ:NHAY

Problem:
http://www.spoj.com/problems/NHAY/
Here is given a simple problem related to string matching often called as a needle in a haystack
This problem is easily solved with Kmp algorithm
Below is the working code for above problem
#include<cstdio>
#include<cstring>
using namespace std;
char pat[1000010];
char text[1000010];
int pref[1000010];
void prefix(char *s)
{
 int n=strlen(s),flag=1;
 int j=0,i;
 pref[0]=0;
 for(int i=1;i<n;i++)
 {
  while(j>0 && s[j]!=s[i])
  j=pref[j-1];
  if(s[i]==s[j]) 
  j++;
  pref[i]=j;
 }
 
 
}
void search(char *text,char *pat)
{
 int len=strlen(text),flag=1;
 int m=strlen(pat);
 int j=0;
 for(int i=0;i<len;i++)
 {
  while(j>0 && text[i]!=pat[j])
  j=pref[j-1];
  if(text[i]==pat[j])
  j++;
  if(j==m)
  {
  printf("%d\n",i-m+1);
  j=pref[j-1];
  }
 }
 if(flag==1)
        printf("\n");
}
int main()
{
 int n;
 while(scanf("%d\n",&n)!=EOF)
        {
      memset(pref,0,sizeof(pref));
      scanf("%s",pat);
  prefix(pat);
  scanf("%s",text);
  search(text,pat);
 }
 
}