character array and printf makes program slow
<p>(Um,My english is not well)   :)</p>

<p>My friend is learning C++ now,and he find a problem that I can't explain why.</p>

<p>The First Code ,it runs more than 2000MS</p>

<pre><code>#include &lt;cstdlib&gt;
#include &lt;cstdio&gt; 
#include &lt;cstring&gt;

using namespace std;

int ans[2000000];
char a[2000000];

int main()
{
    scanf("%s\n",a);
    int l=1,r=strlen(a);
    for (int i=0;i&lt;strlen(a);i++) 
        if (a[i]=='l')
            ans[r--] = i+1;
        else
            ans[l++] = i+1;
    for (int i=1;i&lt;=strlen(a);i++)
        printf("%d\n",ans[i]);
    return 0;
}
</code></pre>

<p>The Second Code,it runs 465MS</p>

<pre><code>#include &lt;cstdlib&gt;
#include &lt;cstdio&gt;
#include &lt;cstring&gt;

using namespace std;

int ans[2000000];
char a[2000000];
int size;

int main()
{
    scanf("%s",a);
    int l=1,r=strlen(a);
    size = r;
    for (int i=0;i&lt;strlen(a);i++) 
        if (a[i]=='l') ans[r--]=i+1;else ans[l++]=i+1;
    for (int i=1;i&lt;=size;i++)
        printf("%d\n",ans[i]);
    return 0;
}
</code></pre>

<p>The Third Code,it runs more than 2000MS</p>

<pre><code>#include &lt;cstdlib&gt;
#include &lt;cstdio&gt;
#include &lt;cstring&gt;

using namespace std;

int ans[2000000];
char a[2000000];
int size;

int main()
{
    scanf("%s",a);
    int l=1,r=strlen(a);
    size = r;
    for (int i=0;i&lt;size;i++) 
        if (a[i]=='l') ans[r--]=i+1;else ans[l++]=i+1;
    for (int i=1;i&lt;=strlen(a);i++)
        printf("%d\n",ans[i]);
    return 0;
}
</code></pre>

<p>The Last Code ,it runs 515MS</p>

<pre><code>#include &lt;cstdlib&gt;
#include &lt;cstdio&gt;
#include &lt;cstring&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

int ans[2000000];
string a;

int main()
{
cin &gt;&gt;a;
    int l=1,r=a.size();
    for (int i=0;i&lt;a.size();i++) 
        if (a[i]=='l') ans[r--]=i+1;else ans[l++]=i+1;
    for (int i=1;i&lt;=a.size();i++)
        printf("%d\n",ans[i]);
    return 0;
}
</code></pre>

<p>When input string size is 10^5</p>

<p>So , the reason is about second for loop.</p>

<p>And my question is why "strlen" function and print in for loop will make the code so slow? </p>
