matavanga's blog

By matavanga, 13 years ago, In English
I can not find out the problem in my code anyone please help..


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <fstream>
using namespace std;
int main ()
{
    int stdnum,l=10000;
   
    string qlt;
  
   
    cin>>stdnum;
    cin>>qlt;
    int ans[stdnum];
   
    ans[0]=1;
    for(int i=0;i<(int)qlt.length();i++)
    {    
        if(qlt[i]=='=')
        ans[i+1]=ans[i];
       
       
        else if(qlt[i]=='R')
        ans[i+1]=ans[i]+1;
       
       
        else if(qlt[i]=='L')
         ans[i+1]=ans[i]-1;       
         }   
           
    for(int i=0;i<stdnum;i++)
            l=min(l,ans[i]);
    if(l<1)
        {
            l=(-1)*l;
               
            for(int i=0;i<stdnum;i++)ans[i]+=l+1;
        }
            for(int i=0;i<stdnum;i++)cout<<ans[i]<<" ";
           
        }
   
  • Vote: I like it
  • +1
  • Vote: I do not like it

13 years ago, # |
  Vote: I like it +1 Vote: I do not like it
  • 13 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    Why such an algorithm works well?
    I want to know the proof of correctness.

    If you have some idea, could you tell me about it?
13 years ago, # |
  Vote: I like it +1 Vote: I do not like it
but whats the logical error in my code? my code is very simple.
13 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it
A simple test
In
5
RLLL
Out
1 4 3 2 1
Your answer: 3 4 3 2 1