有一个单词 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. sublime3 install python3

    链接地址:https://blog.csdn.net/Ti__iT/article/details/78830040

  2. Python 之 风格规范(Google )

    开头先mark一下网址:goole官网 任何语言的程序员,编写出符合规范的代码,是开始程序生涯的第一步. 一.分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 二.行长度 每行不超过80个 ...

  3. 根据业务自己设计的.NET工厂模式架构

    最近项目的架构需要做调整优化,根据业务需要写了一个简单的工厂模式架构 项目介绍:整个系统分为三大平台(这里用A,B,C来标示),每个平台又细分为多个APP客户端(每个APP都有appid来区分) 因为 ...

  4. 还是UVa340

    #include<stdio.h> #define maxn 1010 int main() { int num,a[maxn],i,j,b[maxn]; ; &&num) ...

  5. Select, Poll,Epoll

    Date: 2019-06-19 Author: Sun 1. Select ​ select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当se ...

  6. JS 用+1、-1填12()34()56()78()9=59

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 路飞学城Python-Day181

    Evernote Export Nginx默认网站 当Nginx配置文件中有且仅有一个Server的时候,该Server就被Nginx认为是默认网站,所有发给Nginx服务器80端口的数据都会默认给s ...

  8. 路飞学城Python-Day100

    Django项目之图书馆项目 1.项目架构 2.表结构设计 from django.db import models # Create your models here. #作者详情表 class A ...

  9. tomcat 配置 https 几点注意

    1.修改server.xml时候把注释的改改就好,不要添加,免得杂乱. 2.安装openssl openssl-devel autoconf libtool apr tomcat-native 才行. ...

  10. linux下查看mysql版本的四种方法

    Linux查看MySQL版本的四种方法 1 在终端下执行 mysql -V 2 在help中查找 mysql --help |grep Distrib 3 在mysql 里查看 select vers ...