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 ...
随机推荐
- MSDN性能分析相关网站收集
Visual Studio 性能分析初学者指南 | Microsoft Docshttps://docs.microsoft.com/zh-cn/visualstudio/profiling/begi ...
- webpack入门 --初级压缩
1.新建一个文件夹,再初始化npm: npm init 2.安装webpack,首先要全局安装,再本地安装: npm install webpack -g // 全局安装 npm install we ...
- Vue.js 2 vs Vue.js 3的实现 – 云栖社区
Vue.js 2 vs Vue.js 3的实现 – 云栖社区 vue.js核心团队已经讨论过将在Vue3实现的变化.虽然API不会改变,但是数据响应机制(译者注:对数据改变的监听和通知)发生了变化.这 ...
- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build*解决办法
easy_install -U setuptools or pip install ipython 亲测有效
- jquery插件生成简单二维码
除了利用第三方网站生成二维码外,这是一个比较简单的办法吧. <script src="/Scripts/jquery.qrcode.min.js" type="te ...
- Ubuntu终端命令行缩短显示路径
平时我们使用linux终端命令行的时候,常常会被一个问题困扰,那就是文件路径过长, 有时候甚至超过了一行,这样看起来非常别扭,其实只要两步就可以解决这个问题: 1,修改.bashrc文件(用户根目录下 ...
- 【airtest】iOS,Android 依托 jenkins 并行跑
Airtest 只支持一台mac 连接一台iPhone, 以下方法是以“一台mac 连接一台iPhone”为基础,依托jenkins 统一管理多台iPhone. [mac] jenkins mast ...
- Nginx全局变量
一.全局变量 $args #请求中的参数值 $query_string #同 $args $arg_NAME #GET请求中NAME的值 $is_args #如果请求中有参数,值为"?&qu ...
- Python数据分析--------numpy数据打乱
一.shuffle函数: import numpy.random def shuffleData(data): np.random.shufflr(data) cols=data.shape[1] X ...
- C++基础 (2) 第二天 C++相对C的改进 命名空间 引用
1 昨日回顾 2内联函数 3 默认参数和占位参数 4函数重载 函数重载 就是可以定义多个相同名字的函数 6 类和对象的基本语法 7 类的封装和访问控制 还有一个结论: 封装有两层含义: 把属性和方法进 ...