有一个单词 W,输出它在字符串 S 中从左到右第一次出现的位置 IDX(设 S 中的第 1 个字符的位置为 1)。W 只由英文字母组成,S 除英文字母和汉字之外在任何位置(包括头和尾)另有一个或多个连续的空格。

查找单词时,不区分大小写,但要求完全匹配,即单词 W 必须与 S 中的某一独立单词在不区分大小写的情况下完全匹配。W 仅是 S 中某一单词的一部分就不算匹配。

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define length 1000001
void strlwr(char *s)
{
char *p=s;
while(*p)
{
if(*p>='A'&&*p<='Z')
*p=*p+;
p++;
}
}
int main()
{
  int num;
  scanf("%d", &num);
  for (int s = ; s < num; s++)
  {
  char words[], sen[length];   scanf("%s", words);
  getchar();
  gets(sen);   strlwr(words);
  strlwr(sen);   int len = strlen(words),lens = strlen(sen);
  sen[lens] = ' ';
  sen[lens + ] = ;   char *p,*ps = sen;
  while ()
  {
  p = strstr(ps, words);
  if (p == )
  break;
  if (p == sen&&*(sen + len) == ' ')
  break;
  if (*(p - ) == ' '&&*(p + len) == ' ')
  break;
  ps = p + len;
  }   if (p == )
  printf("case #%d:\nNone\n", s);
  else
  {
  int pos = p - sen + ;
  printf("case #%d:\n%d\n", s, pos);
  }
  }
return ; }

C++实现

 #include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
string strlwr(string a){
for(int i=;i<a.size();i++)
a[i]=tolower(a[i]);
return a;
}
int main()
{
int T;scanf("%d\n",&T);
for(int m=;m<T;m++){
string a,ss;cin>>a;getchar();
getline(cin,ss);
a=strlwr(a);ss=strlwr(ss);
ss+=' ';ss.insert(," "); int pos=,tmp=,l=a.size(),flag=;
while((tmp=ss.find(a))!=-){
pos+=tmp;
if(ss[tmp-]==' '&&ss[tmp+l]==' '){
flag=;
break;
} else{
ss.erase(,tmp+l);
pos+=l;
}
}
if(flag) printf("case #%d:\nNone\n",m);
else printf("case #%d:\n%d\n",m,pos);
}
return ;
}

以上是我的代码,思路是一样的,C可以控制strstr从指针处开始寻找,而C++的find似乎没有从字符串的某一位置开始找的功能,因此只好用erase删除,并且还要注意后续对pos的操作。

然后在讨论区看到了更精简的,直接在find的时候添加空格以此确定是不是单独的单词就行了!

 #include <bits/stdc++.h>
using namespace std;
int main(){
int t,k;
string s,s1;
cin>>t;
getchar();
for(int i=;i<t;++i){
getline(cin,s);
getline(cin,s1);
for(int j=;j<s.length();++j) s[j]=tolower(s[j]);
for(int j=;j<s1.length();++j) s1[j]=tolower(s1[j]);
if(s1.find(s+" ")==) cout<<"case #"<<i<<":"<<endl<<<<endl;
else if(s1.find(" "+s+" ")!=-){
k=s1.find(" "+s+" ");
cout<<"case #"<<i<<":"<<endl<<k+<<endl;
}
else if(s1.find(s+" ")!=-) cout<<"case #"<<i<<":"<<endl<<s1.find(s+" ")+<<endl;
else cout<<"case #"<<i<<":"<<endl<<"None"<<endl;
}
}

(以上1,3代码来自讨论区和数据区http://acm.ecnu.edu.cn/problem/3018/statistics/)

EOJ 3018 查找单词的更多相关文章

  1. python制作查找单词翻译的脚本

    本人由于英语渣,在linux底下经常看文档,但是有没有想有道词典这种软件,所以遇到不懂的单词只能手动复制粘贴在网上查找,这样就很不方便,学了python之后,就试着自己尝试下个在命令行下查找单词翻译的 ...

  2. vim 精确匹配查找单词【转】

    删除文件中所有的空行:g/^\s*$/d 去掉所有的行尾空格::%s/\s\+$// 整个文件特定字符串的替换:%s/old_word/new_word/g 删除从当前行开始到最后一行的所有内容:., ...

  3. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  4. [leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  5. hdu 1251 字典树模板题 ---多串 查找单词出现次数

    这道题题目里没有给定数据范围 我开了2005  疯狂的WA 然后开了50000, A掉  我以为自己模板理解错  然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...

  6. 79. Word Search在字母矩阵中查找单词

    [抄题]: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...

  7. [LintCode] Add and Search Word 添加和查找单词

    Design a data structure that supports the following two operations: addWord(word) and search(word) s ...

  8. 79. 212. Word Search *HARD* -- 字符矩阵中查找单词

    79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...

  9. word search(二维数组中查找单词(匹配字符串))

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

随机推荐

  1. lua 计算字符串字符个数“中文字算一个字符”

    local function GetStringWordNum(str) local fontSize = local lenInByte = #str local count = local i = ...

  2. 用 JS + LeanCloud 给网页添加数据库(留言功能)

    记录给自己网页添加留言功能的过程. 使用工具:LeanCloud,一个自带数据库和增删改查(CRUD)功能的后台系统. 1 在JS中引入LeanCloud官方库 在LeanCloud注册并添加应用的步 ...

  3. JavaScript操作HTML&CSS简单入门

    - Java攻城狮学习路线 - 一. JavaScript基础 输出 使用 window.alert() 弹出警告框. 使用 document.write() 方法将内容写到 HTML 文档中. 使用 ...

  4. 基于 Web 的 Go 语言 IDE - Wide 1.4.0 发布!

    Wide 是什么 Wide 是一个基于 Web 的 Go 语言团队 IDE . 在线开发:打开浏览器就可以进行开发.全快捷键 智能提示:代码自动完成.查看表达式.编译反馈. Lint 实时运行:极速编 ...

  5. MongoDB 博客截图之一

    来源:十天掌握MongoDB之三:学会Find - 学吧网 - 专注于PHP资源分享

  6. 时空上下文视觉跟踪(STC)

    论文的关键点是对时空上下文(Spatio-Temporal Context)信息的利用.主要思想是通过贝叶斯框架对要跟踪的目标和它的局部上下文区域的时空关系进行建模,得到目标和其周围区域低级特征的统计 ...

  7. css table布局

    表格布局有两种方式: 1.HTML Table(<table>标签)和 2. CSS Table(display:table 等相关属性). HTML Table是指使用原生的<ta ...

  8. window环境下,提升工作效率

    效率工具 windows桌面程序 Listary 本地文件搜索器 有道词典 划词翻译, 并且能够存在生词本里面 印象笔记 浏览资料整理 chrom插件 crxMouse 鼠标手势 chrome vim ...

  9. 01--[转]C++强大背后

    [转]C++强大背后 2014-01-22 分类:互联网 阅读(9295) 评论(6) 在31年前(1979年),一名刚获得博士学位的研究员,为了开发一个软件项目发明了一门新编程语言,该研究员名为Bj ...

  10. Spring中xml文档的schema约束

    最开始使用Spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意.最近想着寻根问底的探究一下.以下是本文主要内容: 1.配置文件示例.   <?xml version= ...