有一个单词 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. APM技术原理

    链接地址:http://www.infoq.com/cn/articles/apm-Pinpoint-practice 1.什么是APM? APM,全称:Application Performance ...

  2. Tomcatsession共享方案--memcached-session-manager

    https://github.com/magro/memcached-session-manager/wiki/SerializationStrategies     MSM的特性:    a.支持t ...

  3. vue路由history模式下打包node服务器配置

    vue-router 默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载. 如果不想要很丑的 hash,我们可以用路由的 his ...

  4. mybatis 高级映射和spring整合之高级映射(4)

    mybatis 高级映射和spring整合之高级映射 ----------------学习结构-------------------- 0.0 对订单商品数据模型进行分析 1.0 高级映射 1.1 一 ...

  5. spring之pom.xml配置

    spring之pom.xml配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  6. [ Linux ] [ OS ] [ memory ] Linux 如何查看系統硬體的記憶體(RAM)資訊

    cat /proc/meminfo https://blog.longwin.com.tw/2013/05/linux-ram-memory-info-2013/

  7. 关于 Windows 10 如何扩展分区与合并分区

    前言 相信大部分人都遇见磁盘不够用的问题吧,然后都在后悔当初为什么就给 x 盘分了 10G 的容量吧. 不过没关系,自从 Windows 7 开始( xp 我也不知道有毛有),Windows 自带的磁 ...

  8. mac安装win10后触摸板没有右键功能键的添加技巧

    一些mac用户也会在自己的笔记本电脑上安装windows10系统. 但最近有部分用户发现,安装上win10正式版后,发现无论点击触摸板哪个位置,都只有左键,根本无法右键的问题, 针对此问题,现笔者分享 ...

  9. 序列模型(2)-----循环神经网络RNN

    一.RNN的作用和粗略介绍: RNN可解决的问题: 训练样本输入是连续的序列,且序列的长短不一,比如基于时间的序列:一段段连续的语音,一段段连续的手写文字.这些序列比较长,且长度不一,比较难直接的拆分 ...

  10. Python数据分析--------numpy数据打乱

    一.shuffle函数: import numpy.random def shuffleData(data): np.random.shufflr(data) cols=data.shape[1] X ...