/*<说明>
编程实现将字符串中最短的单词输出,在主函数中输入字符串,编写一个函数完成最短单词的查找
</说明>*/
#include<time.h>
#include<iostream>
using namespace std; void shortestWord(char* in)
{
int i,j=;
int o[];
for(i=;*(in+i)!=;i++)
{
if(*(in+i)==' ')
{
o[j]=i; //o的作用是定位每一个空格的位置 通过空格位置间隔的大小判断单词的长短
j++;
}
}
j--;
int z=;
int k=o[]; //k是最短单词的个数 初始化为第一个空格的位置 因为后面的操作没有考虑第一个
int l[];
for(;j!=;j--)
{
if(o[j]-o[j-]-<k)
{
z=;
k=o[j]-o[j-]-;
l[]=j-;
}
else if(o[j]-o[j-]-==k)
{
z++; //z统计有多少个最短单词
l[z]=j-;
}
}
if(o[]==k)
{
for(int n=;n<k;n++)
{
printf("%c",*(in+n)); //如果第一个单词就是最短的 打印
}
printf(" ");
}
for(int m=z;m>;m--)
{ for(int n=;n<=k;n++)
{
printf("%c",*(in+o[l[m]]+n)); //打印其他最短单词
}
printf(" ");
}
}
void main()
{
char in[]="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=;
char *findshort(char s[])
{
static char s1[Max]; //其地址要返回,所以设计为静态变量
char s2[Max];
int i=,j,len1=,len2=;
while(s[i++]!='\0');
s[i-]=' ';
s[i]='\0';
i=;
while(s[i]!='\0')
{
if(s[i]==' '&&s[i+]!='\0'&&s[i+]==' ') //跳过多余空格
{
i++;
continue;
}
if(s[i]!=' ') //提取一个单词到S2中
{
s2[len2++]=s[i];
}
else if(len1==)
{
len1=;
for(j=;j<len2;j++) //将S2复制到S1中
s1[len1++]=s2[j];
s1[len1]='\0';
len2=;
}
else if(len1>len2)
{
len1=;
for(j=;j<len2;j++) //将S2复制到S1中
s1[len1++]=s2[j];
s1[len1]='\0';
len2=;
}
else
{
len2=;
}
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();
}

这是答案中的 只打印了第一个最短单词 但是实现比自己写的代码快很多。

C++基础练习题(一): 查找最短单词的更多相关文章

  1. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  2. [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  3. noi25 最长最短单词(为什么会出现运行时错误)

    noi25 最长最短单词(为什么会出现运行时错误) 一.总结 一句话总结:比如除以零,数组越界,指针越界,使用已经释放的空间,数组开得太大,超出了栈的范围,造成栈溢出 1.c++报runtime er ...

  4. js基础练习题(1)

    1.字符串 视频教程地址: js基础练习题 1.如何连接两个或者两个以上字符串? var cssname = 'box' var num = 1 var html = '<div class=& ...

  5. Linux基础练习题(二)

    Linux基础练习题(二) 1.复制/etc/skel目录为/home/tuer1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限. [root@www ~]# cp -r ...

  6. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  7. AC日记——最长最短单词 openjudge 1.7 25

    25:最长最短单词 总时间限制:  1000ms 内存限制:  65536kB 描述 输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母.空格和逗号.单词由至少一个连续的字母构成 ...

  8. [Swift]LeetCode244.最短单词距离 II $ Shortest Word Distance II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  9. [Swift]LeetCode245.最短单词距离 III $ Shortest Word Distance III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

随机推荐

  1. solr多条件查询(三)

    1.昨天记了一下三条件的“并且” “并且”(  &&   &&  )的情况,今天再来记一下 “并且”  “或者” 的情况. 这里的或者情况,一定要搞清楚无论有多少情况, ...

  2. zookeeper集群配置与启动——实战

    1,准备: A:三台linxu服务器: 10.112.29.177 10.112.29.172 10.112.29.174 命令 hostname 得到每台机器的 hostname vm-10-112 ...

  3. nyoj 4 ASCII码排序 java

    java输入字符:1.String s=sc.next(); 2.char a=s.charAt(0); 注意:package   java 中提交不能带package java代码: import ...

  4. Eclipse常见配置及常用插件

    tomcat为能同时运行多个项目而不崩溃,需要配置一下jvm设置 -Xms1024m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m

  5. oracle数据表创建分区与查询

    场景: 遇到1亿数据量的数据需要根据用户名做些数据统计分析,想直接做些聚合计算基本没可能,于是打算先根据日期按照年月创建分区,然后对各个分区分别进行统计,最后汇总结果. 有两种方法,分别是手工设置分区 ...

  6. Handler Should be static or leaks Occur?

    解决办法: public class SampleActivity extends Activity { /** * Instances of static inner classes do not ...

  7. Android 实现简单音乐播放器(二)

    在Android 实现简单音乐播放器(一)中,我介绍了MusicPlayer的页面设计. 现在,我简单总结一些功能实现过程中的要点和有趣的细节,结合MainActivity.java代码进行说明(写出 ...

  8. mysql基础面试

    php面试题之五--MySQL数据库(基础部分) 五.MySQL数据库 mysql_num_rows() mysql_affected_rows() 这两个函数都作用于 mysql_query($qu ...

  9. Ubuntu 下apache2开启rewrite隐藏index.php

    为了实现 http://www.example.com/route/route 而不是 http://www.example.com/index.php/route/route 需要开启apache2 ...

  10. UNIX Time 时间戳 与 北京时间 相互转换

    typedef struct t_xtime { int year; int month; int day; int hour; int minute; int second; } _xtime ; ...