3018. 查找单词

10175101121

用C的话,gets()不能用,strlwr()不能用,全要自定义,不然一直compile error。

10175101121

现在的确可以用gets(),不过一开始做的时候好像不行。。。

10175101103-STARK

gets()是可以用的,不过strlwr和strupr不是标准C库函数,只能在VC中使用。linux gcc环境下需要自行定义这个函数。

10152130146

竟然会超时
C++ :

include

include

include

using namespace std;
int main()
{
int n,i;string a,b;
cin>>n;getline(cin,a);
for(i=0;i<n;i++)
{cout<<”case #” << i <<”:” <<endl;
getline(cin,a);getline(cin,b);
transform(a.begin(), a.end(), a.begin(), ::toupper);
transform(b.begin(), b.end(), b.begin(), ::toupper);
if(b.find(a+” “)==0)cout<<b.find(a+” “)+1<<endl;
else if(b.find(” “+ a+” “)!=string::npos)cout<<b.find(” “+a+” “)+2<<endl;
else if(b.find(” “+ a)==b.length()-a.length()-1)cout<<b.length()-a.length()+1<<endl;
else cout<<”None”<<endl;
}
return 0;
}

怎么会超时!!??
[em:02]
OJ服务器性能太弱了!
现在电脑速度都那么快了,你们也要考虑历史的进程啊!

10175102213

“S 除英文字母和汉字之外在任何位置(包括头和尾)另有一个或多个连续的空格”
这句话的意思应该是头尾至少有一个空格吧。
一直搞不定看了一下测试数据,有一些开头完全没有空格啊!是我语文没学好吗QAQ

改个名字吧

提醒一下 测试样例里的W并没有满足小于十 所以代码里的W如果用字符数组存的时候记得开大点。。。

kingno

数据太牛了

..空白..

w要比10大。。

include

include

include

int main()
{
int num;
int i,j,k,l1,l2,m,n,boo;
scanf(“%d”,&num);
for(i=0;i<num;i++){
char w[101];
char s[1000001];
scanf(“%s”,w);
getchar();
gets(s);
l1=strlen(w);
l2=strlen(s);
printf(“case #%d:\n”,i);
for(j=0;j=’A’)&&(w[j])<=’Z’)w[j]=w[j]+32;
if((s[j]>=’A’)&&(s[j])<=’Z’)s[j]=s[j]+32;
}
for(j=0;j<=l2-l1;j++){
k=0;
n=0;
if((s[j+l1]>=’A’)&&(s[j+l1])<=’Z’)s[j+l1]=s[j+l1]+32;
if ((((s[j-1]<’a’)||(s[j-1]>’z’))||j==0)&&((s[j+l1]<’a’)||(s[j+l1]>’z’)||(j==(l2-l1)))){
boo=0;

            for(m=j;m<j+l1;m++){
                if(s[m]==w[k])boo++;
                k++;
                }
        if(boo==l1){
        printf("%d\n",j+1);
        n=1;
        break;}
        }

    }
    if(n==0)printf("None\n");
    /*printf("%s\n",w);
    printf("%d\n",l);
    printf("%s\n",s);*/

}

}

10165101292

没想到居然会成功…虽然也不知道大家会不会需要,我发一下。
膜拜大佬,不过代码写的有点乱了,这是我的,AC了

include

include

define length 1000001

int main()
{
int num;
scanf(“%d”, &num);
for (int s = 0; s < num; s++)
{
char words[11], sen[length];

    scanf("%s", words);
    getchar();
    gets(sen);

    _strlwr(words);
    _strlwr(sen);

    int len = strlen(words);

    int lens = strlen(sen);
    sen[lens] = ' ';
    sen[lens + 1] = 0;

    char *p;
    char *ps = sen;

    while (1)
    {
        p = strstr(ps, words);
        if (p == 0)
            break;
        if (p == sen&&*(sen + len) == ' ')
            break;
        if (*(p - 1) == ' '&&*(p + len) == ' ')
            break;
        ps = p + len;
    }

    if (p == 0)
        printf("case #%d:\nNone\n", s);
    else
    {
        int pos = p - sen + 1;
        printf("case #%d:\n%d\n", s, pos);
    }
}
return 0;

}

徐摆渡
#include <bits/stdc++.h>
using namespace std;
//模拟注意判断几个边界条件

int T, cnt, flag, ans;
char obj[200];//体面说的w长度不超过10 结果测试点10就超了10……,数组还是开大的好
char str[1000005];

void conver(char *s)//将所有字母全部转化成大写
{
    int len=strlen(s);
    for(int i=0;i<len;++i)
        if(s[i]>='a'&&s[i]<='z') s[i]-=32;
}
int main()
{
    scanf("%d\n",&T);
    while(T--){
        ans=0;
        gets(obj);
        gets(str);
        conver(obj);
        conver(str);
        int len1=strlen(obj);
        int len2=strlen(str);
        int j, k;
        for(int i=0;i<len2;++i){
            if(str[i]==obj[0]){
                j=i, k=0, flag=0;
                while(k<len1)
                    if(str[j++]!=obj[k++]) {flag=1;break;}
                //cout<<i<<' '<<j<<endl;
                if(!flag){
                    if(i>=1&&str[i-1]==' '&&j==len2) {ans=i+1;break;}
                    if(i==0&&str[j]==' ') {ans=i+1;break;}
                    if(i==0&&j==len2) {ans=i+1;break;}
                    if(i>=1&&str[i-1]==' '&&str[j]==' ') {ans=i+1;break;}
                }
                i=j;
            }
        }
        printf("case #%d:\n",cnt++);
        if(ans) printf("%d\n",ans);
        else printf("None\n");
    }
    return 0;
}
10152130230

来个大神教我哪里错了

include

include

include

int main()
{
int t,i,y,k,j,lens,u;
char w[15]={0},s[1000005]={0};
scanf(“%d”,&t);
for(i=0;i<t;i++)
{
memset(w,0,sizeof(w));
memset(s,0,sizeof(s));
scanf(“%s”,w);
getchar();
gets(s);
y=strlen(w);
for(k=0;k0;k–)
{
w[k]=w[k-1];
}
w[0]=’ ‘;
w[y+1]=’\0’;
char *x=strstr(strlwr(s),strlwr(w));
printf(“case #%d:\n”,i);
y=strlen(w);
lens=strlen(s);
for(j=0;j<=lens-1;j++)
{
if(x==&s[j])
break;
}
if(x!=0&&(s[j+strlen(w)]==’ ‘||s[j+strlen(w)]==’.’))
printf(“%d\n”,j+2);
else printf(“None\n”);
}
for(u=j+y;u<=lens-1;u++)
s[u-(j+y)]=s[u];
s[lens-(j+y)]=’\0’;
}
return 0;
}

Fifnmar

这道题为什么那么高的 EMB?吓了我一跳。我代码的思路应该很明白了,必要的地方加了注释。还是老规矩,为了不占太多版面我删了一些括号,不过我实际写的代码即使 if 等内层语句只有一条也会加括号的。

#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;

int main() {
    int t; cin >> t; cin.get();
    for (int query = 0; query < t; ++query) {
        string target, text;
        getline(cin, target);
        getline(cin, text);
        for (auto &i : target)
            i = tolower(i);
        for (auto &i : text)
            i = tolower(i);
        text.push_back(' '); // Sentinel, for simplicity.
        auto const bg = text.begin();
        auto const ed = text.end();
        auto i = bg;
        struct Word {
            size_t start_pos;
            string word;
        };
        vector<Word> words;
        while (true) {
            while (*i == ' ') {
                // Safely assume string is terminated by '\0'.
                ++i;
            }
            if (i == ed)
                break;
            auto j = i;
            while (*j != ' ') {
                // Safely assume string has trailing spaces.
                ++j;
            }
            words.push_back({static_cast<size_t>(i - bg), {i, j}});
            i = j;
        }
        cout << "case #" << query << ":\n";
        bool found = false;
        for (auto const& word_rec : words) {
            if (word_rec.word == target) {
                cout << word_rec.start_pos + 1 << '\n';
                found = true;
                break;
            }
        }
        if (found == false)
            cout << "None\n";
    }
}
10175101115

题目里的example的output有错吧,case 2应该也是29,以为自己错了没想到ac了
另外似乎直接读取整行并不会超时啊,可能最近更新过了吧

10175102262 LarsPendragon

output没错,题目里提示了行首4个空格

kcisemedamuoy

没想到居然会成功…虽然也不知道大家会不会需要,我发一下。

include

include

include

char wordbuf[11],strbuf[1000001];
char * mystrstr(char ,char );
void calculate(int);
int main(){
int t;
scanf(“%d”,&t);
getchar();
calculate(t);
}
char * mystrstr( char strbuf, char wordbuf){
int len1=strlen(strbuf),i,count=0,len2=strlen(wordbuf),j;
for(i=0;i<len1;++i){
if(strbuf[0]==wordbuf[0]){
for(j=1;j<len2;++j){
if(strbuf[j]!=wordbuf[j])break;
else if(j==len2-1&&(strbuf[i+1]==’ ‘||strbuf[i+1]==’.’||strbuf[i+1]==’,’))
return strbuf+1;
}
}
if(count){
if(strbuf[i]!=wordbuf[count]){
count=0;
}else{
++count;
}
}
if((strbuf[i-1]==’ ‘||strbuf[i-1]==’,’||strbuf[i-1]==’.’)&&(strbuf[i]==wordbuf[0])&&count==0){
++count;
}

if(count==len2){
    if(strbuf[i+1]==' '||strbuf[i+1]=='.'||strbuf[i+1]==',')
    return strbuf+i-count+2;
else{
count=0;
}
}

}
return 0;
}
void calculate(int t){
int j=t,i,location;
char p2=strbuf,p1=wordbuf,*temp=strbuf;
while(t–){
gets(wordbuf);
gets(strbuf);
printf(“case #%d:\n”,j-t-1);
p2=strlwr(p2);
p1=strlwr(p1);
location=mystrstr(strbuf,wordbuf)-temp;
if(location<0){
printf(“None\n”);
}else{
printf(“%d\n”,location);
}
}
}

三七茧茧

和答案里case#2的输出不一样。可是还可以ac……不明白

include

include

include

include

include

char strupr(char str)
{
while(str!=’\0’)
{
if(
str>=’a’&&str<=’z’)
str-=32;
str++;
}
return str;
}

int main()
{
int T,i;
scanf(“%d”,&T);
getchar();
for(i=0;i<T;i++)
{
int r;
char a[11],b[1000000];
char m;
a[0]=’ ‘;
scanf(“%s”,&a[1]);
r=strlen(a);
a[r]=’ ‘;
a[r+1]=0;
getchar();
m=getchar();
b[0]=’ ‘;
int j=1;
while(m!=’\n’)
{
b[j++]=m;
m=getchar();
}
b[j] = 0;

    strupr(a);
    strupr(b);
    char *p;
    p=strstr(b,a);
    printf("case #%d:\n",i);
    if(p==NULL)printf("None\n");
    else
        printf("%d\n",p-&b[0]+1);
}
return 0;

}

10175102262 LarsPendragon

和答案不一样是因为直接复制粘贴的时候行首是没有空格的(这里要手动在本地试)

10175101159

直接模拟就过了。
给出一种提高效率的办法
模拟过程中判到首位匹配时
实时判断相应的尾部是不是空格
不是空格就不用继续往下模拟了
ps:吐槽C++的string相关好慢啊
用string,getline(),s.length()用时0.132
代码只把这三个改成char[],gets(),strlen()用时0.016
TuT(逃

zzpzbl

搞不懂我的为啥超时

Li Dao

没想到居然会成功…虽然也不知道大家会不会需要,我发一下。
这程序好像有点bug,
数据:
1
in
in

输出是None。

不过能AC

Li Dao

竟然会超时
std::ios::sync_with_stdio(false);
关闭流同步加速下。
另外,对性能要求高的题目,一般不用cin,cout,string,stringstream

10175102262 LarsPendragon
[已删除]
你当前正在回复 博客/题目
存在问题!