C++基礎練習問題(一):最短単語を検索する

13127 ワード

/*<  >

                ,          ,               

</  >*/

#include<time.h>

#include<iostream>

using namespace std;



void shortestWord(char* in)

{

    int i,j=0;

    int o[1000];

    for(i=0;*(in+i)!=0;i++)

    {

        if(*(in+i)==' ')

        {

            o[j]=i;             //o                                 

            j++;

        }

    }

    j--;

    int z=0;

    int k=o[0];                  //k                                    

    int l[50];

    for(;j!=0;j--)

    {    

        if(o[j]-o[j-1]-1<k)

        {

            z=0;

            k=o[j]-o[j-1]-1;

            l[0]=j-1;

        }

        else if(o[j]-o[j-1]-1==k)

        {

            z++;              //z          

            l[z]=j-1;

        }

    }

    if(o[0]==k)

    {

        for(int n=0;n<k;n++)

        {

            printf("%c",*(in+n));       //               

        }

        printf(" ");

    }

    for(int m=z;m>0;m--)

    {



        for(int n=1;n<=k;n++)

        {

            printf("%c",*(in+o[l[m]]+n));  //        

        }

        printf(" ");

    }

}

void main()

{

    char in[1000]="Learning a the parameters of neural networks is perhaps one of the most well studied problems within the field of machine learning. Early work on backpropagation algorithms showed that the gradient of the neural net learning objective could be computed efficiently and used within a gradient descent scheme to learn the weights of a network with multiple layers of non-linear hidden units. Unfortunately, this technique doesn’t seem to generalize well to networks that have very many hidden layers (i.e. deep networks). The common experience is that gradient-descent progresses extremely slowly on deep nets, seeming to halt altogether before making significant progress, resulting in poor performance on the training a set (under-fitting)";

    int a=clock();

    shortestWord(in);

    int b=clock();

    int c=b-a;

    printf("%d",c);

    getchar();

}

自分で書いたコードの効果はよくありません.2ミリ秒の実行効果が遅すぎて、連続するスペースがあることを考慮していません.
/*<    >*/

#include<iostream>

#include<time.h>

using namespace std;

const int Max=200;

char *findshort(char s[])

{

    static char s1[Max]; //

    char s2[Max];

    int i=0,j,len1=0,len2=0;

    while(s[i++]!='\0');

    s[i-1]=' ';

    s[i]='\0';

    i=0;

    while(s[i]!='\0')

    {

        if(s[i]==' '&&s[i+1]!='\0'&&s[i+1]==' ') //      

        {

            i++;

            continue;

        }

        if(s[i]!=' ')    //       S2 

        {

            s2[len2++]=s[i];

        }

        else if(len1==0)

        {

            len1=0;

            for(j=0;j<len2;j++)    // S2   S1 

                s1[len1++]=s2[j];

            s1[len1]='\0';

            len2=0;

        }

        else if(len1>len2)

        {

            len1=0;

            for(j=0;j<len2;j++)    // S2   S1 

                s1[len1++]=s2[j];

            s1[len1]='\0';

            len2=0;

        }

        else

        {

            len2=0;

        }

        i++;

    }

    return s1;

}

void main()

{

    char s[Max]="asddddd gg las sdlgaw va eg aoeng a ge a e gae geoia ae x eox ge x ieg ns e a dfge qdn i am ver";

    cout<<"";

    

    int a=clock();

    cout<<""<<findshort(s)<<endl;

    int b=clock();

    int c=b-a;

    cout<<c<<endl;

    getchar();

}

これは答えの中の最初の最短単語しか印刷されていませんが、自分が書いたコードよりずっと速いです.