EOJ 3018 查找单词
有一个单词 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 查找单词的更多相关文章
- python制作查找单词翻译的脚本
本人由于英语渣,在linux底下经常看文档,但是有没有想有道词典这种软件,所以遇到不懂的单词只能手动复制粘贴在网上查找,这样就很不方便,学了python之后,就试着自己尝试下个在命令行下查找单词翻译的 ...
- vim 精确匹配查找单词【转】
删除文件中所有的空行:g/^\s*$/d 去掉所有的行尾空格::%s/\s\+$// 整个文件特定字符串的替换:%s/old_word/new_word/g 删除从当前行开始到最后一行的所有内容:., ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [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 ...
- hdu 1251 字典树模板题 ---多串 查找单词出现次数
这道题题目里没有给定数据范围 我开了2005 疯狂的WA 然后开了50000, A掉 我以为自己模板理解错 然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...
- 79. Word Search在字母矩阵中查找单词
[抄题]: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...
- [LintCode] Add and Search Word 添加和查找单词
Design a data structure that supports the following two operations: addWord(word) and search(word) s ...
- 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 ...
- word search(二维数组中查找单词(匹配字符串))
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
随机推荐
- 2018最新WordPress缩略图设置方法
缩略图设置的方法很多,但都不全面,且很多教程已经失效了,其中使用插件来实现,可是那些插件都使用过都不能实现效果,所以我整理了一份使用代码实现缩略图的方法. 1.找到网站根目录/wp-content/t ...
- Laravel5 开启Debug
Laravel默认没有开启Debug,打开需要两步: .env文件中设置APP_DEBUG=true: 把Laravel服务重启一下(摸索出来的). 这只是开启了基本的调试模式,如果需要看更详细的调试 ...
- 搜索关键词和类目url简短化
电商网站中有很多搜索关键词或者类目的url往往是一大串的url:有个需求是将长的url如: 域名+/products.html?q=大侠&showtype=img&sort=isTra ...
- setTimeout()传带有参数的函数
w3cshool里的解释:setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,语法:setTimeout(code,millisec). 也就是说,第一个参数可以是字符串形式的Ja ...
- React从安装到实战
建议:初学者看之前请先看一遍菜鸟教程 可以安装一个ATOM编辑器,本人觉得很好用 一.安装并启动项目:网址 搭建好的项目目录为: 二.开始写项目: 1.组件到界面流程: 定义一个组件app.js导出 ...
- JS中的map
定义和用法: map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值. map() 方法按照原始数组元素顺序依次处理元素. 注意: map() 不会对空数组进行检测. 注意: m ...
- Javase 简单练习
public class Test10 { public static void main(String[] args) { System.out.println("------------ ...
- 前端开发—CSS 盒子、浮动、定位
盒子模型 margin padding border content margin: 用于控制元素与元素之间的距离:body自带 8 像素的margin 需要手动去除.(快递盒之 ...
- Python笔记5----集合set
1.集合的概念:无序不重复 分为可变集合(set())和不可变集合(frozenset)两种 2.创建集合 aset=set('hello') >>aset={'h','e','l','o ...
- mmap,malloc分配随机内存
随机数1G #cat malloc_rand_1g.c #include <stdio.h> /* printf, scanf, NULL */ #include <stdlib.h ...