题解

31 条题解

  • -1
    @ 2016-11-15 21:53:14

    理清思路 考虑到所有情况
    #include <cstdio>
    #include <cstring>

    int same(char x,char y){
    if('A'<=x&&x<='Z')
    x=x-'A'+'a';
    if('A'<=y&&y<='Z')
    y=y-'A'+'a';
    return x==y;
    }

    int n=0,count=0,p=-1;
    char c,ref[15],a[10000010];

    int main(){
    // freopen("in.txt","r",stdin);
    scanf("%s",ref);
    scanf("%*c");
    while(scanf("%c",&a[n])!=EOF)

    n++;
    a[n]=' ';
    //flag=-1 这个单词不符合
    //flag>0 正在检查这个单词的第flag位
    //flag=0 等待下一个单词

    int flag=0;
    for(int i=0;i<=n;i++){
    if(flag==-1){
    if(a[i]==' ')
    flag=0;
    else
    continue;
    }
    if(flag>0){
    if(a[i]==' '){
    if(flag==strlen(ref)){
    count++;
    if(p==-1)
    p=i-flag;
    }

    flag=0;
    }
    else if(same(ref[flag],a[i]))
    flag++;
    else
    flag=-1;
    }
    if(flag==0){
    if(a[i]==' ')
    continue;
    else{
    if(same(ref[0],a[i]))
    flag=1;
    else
    flag=-1;
    }
    }
    }
    if(count==0)
    printf("-1");
    else
    printf("%d %d",count,p);
    return 0;
    }

  • -1
    @ 2016-08-01 18:24:19
    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cctype>
    #include <cstdlib>
    #include <vector>
    #include <queue>
    #include <sstream>
    #define min(a,b) (a>b?b:a)
    #define max(a,b) (a>b?a:b)
    
    using namespace std;
    typedef long long lg;
    #define INF 1<<30 
    
    int n,k,m,ans,ans1=1,pos;
    
    string s,ss;
    
    int main()
    {
        //int n=string::npos;
        //cout<<n;
        ios::sync_with_stdio(0);
        getline(cin,s);
        s="#"+s;
        s+='#';
        //cout<<s;
        //cout<<s.size();
        for(int i=0;i<s.size();i++)
        {
            s[i]=toupper(s[i]);
        }
        getline(cin,ss);
        ss="#"+ss;
        ss+='#';
        //cout<<s<<endl<<ss<<endl;
        int t=ss.size();
        for(int i=0;i<t;i++)
        {
           ss[i]=toupper(ss[i]);
           if(ss[i]==' ')ss[i]='#';
        }
        pos=ans=ss.find(s); 
        while(pos!=-1)
        {
            pos=ss.find(s,pos+1);
            if(pos!=-1) ans1++;
        } 
        if(ans!=-1)cout<<ans1<<" ";
        cout<<ans;
        return 0;
    }
    
  • -1
    @ 2016-07-08 21:49:34

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int main()
    {
    char a[101],b[1000101];
    cin>>a;
    gets(b);
    gets(b);
    int x=strlen(a),y=strlen(b),ans=-1,g=0;
    for(int i=0;i<x;i++)
    if(a[i]>='A'&&a[i]<='Z')
    a[i]+=32;
    int m[1000000],n=1;
    m[0]=-1;
    for(int i=0;i<y;i++)
    if(b[i]>='A'&&b[i]<='Z')
    b[i]+=32;
    for(int i=0;i<y;i++)
    if(b[i]==' '){m[n]=i;n++;}
    m[n]=y;
    for(int i=0;i<n;i++)
    {
    char k[20000];
    int t=0;
    for(int j=m[i]+1;j<m[i+1];j++)
    {k[t]=b[j];t++;}
    k[t]=0;
    //cout<<k<<endl;
    if(strcmp(a,k)==0)
    {
    if(ans==-1)
    ans=m[i]+1;
    g++;
    }
    }
    if(ans!=-1)
    cout<<g<<' '<<ans;
    else
    cout<<-1;
    return 0;
    }

  • -1
    @ 2015-12-06 16:05:48

    var
    x,y,s:ansistring;
    m,n,i,lx,ly:longint;
    begin
    readln(x);
    readln(y);
    x:=' '+x+' ';
    lx:=length(x);
    x:=upcase(x);
    y:=' '+y+' ';
    ly:=length(y);
    y:=upcase(y);
    m:=pos(y,x)-1;
    if w<>-1 then
    for i:=1 to ly do
    begin
    n:=copy(s,i,lx);
    if n=x then inc(t);
    end;
    if m=-1 then write('-1') else write(t,' 'm);
    end.
    令我瞠目结舌,字符串这么厉害,以后要好好学学

  • -1
    @ 2015-08-28 08:24:44

    {用哈希做一下}

    var
    a,b:array[0..10000000]of longint;
    i,t1,ans,g,k,len:longint;
    s:ansistring;
    c:char;
    procedure init;
    begin
    t1:=0;
    readln(s);
    s:=lowercase(s);
    k:=0;
    for i:=1 to length(s) do
    begin
    inc(k);
    t1:=(t1+sqr(ord(s[i]))*k)mod 19797797;
    end;
    readln(s);
    s:=lowercase(s);
    i:=0;
    g:=0;
    fillchar(a,sizeof(a),0);
    fillchar(b,sizeof(b),0);
    len:=length(s);
    while len<>i+1 do
    begin
    k:=0;
    if len<>i+1 then
    if s[i+1]<>' ' then
    begin
    inc(g);
    inc(k);
    b[g]:=i;
    a[g]:=(a[g]+sqr(ord(s[i+1]))*k)mod 19797797;
    inc(i);
    if len<>i+1 then
    while s[i+1]<>' ' do
    begin
    inc(k);
    a[g]:=(a[g]+sqr(ord(s[i+1]))*k)mod 19797797;
    inc(i);
    if len=i+1 then break;
    end;
    end;
    if len<>i+1 then
    while s[i+1]=' ' do
    begin
    inc(i);
    if len=i+1 then break;
    end;
    end;
    end;
    procedure work;
    var
    i:longint;
    begin
    ans:=0;
    for i:=1 to g do if a[i]=t1 then inc(ans);
    if ans=0 then
    begin
    writeln(-1);
    halt;
    end;
    write(ans,' ');
    for i:=1 to g do if a[i]=t1 then
    begin
    writeln(b[i]);
    break;
    end;
    end;
    begin
    init;
    work;
    end.

  • -1
    @ 2015-08-21 17:22:23

    var z,x,s:string;
    w,t,i,lx,ls:longint;
    begin
    readln(x);x:=' '+x+' ';lx:=length(x);x:=lowercase(x);
    readln(s);s:=' '+s+' ';ls:=length(s);s:=lowercase(s);
    w:=pos(x,s)-1;
    if w<>-1 then
    for i:=1 to ls do begin
    z:=copy(s,i,lx);
    if z=x then inc(t);
    end;
    if w=-1 then write(-1) else write(t,' ',w);
    end.

    • @ 2015-08-21 17:23:11

      一开始还不知道要用ansistring
      还WA了两次
      不过题确实水啊。。。。。。。。。。。。。。

  • -1
    @ 2015-02-09 20:19:39

    判断大小写都可以
    int d = word[j] - a[i + j];
    if (d!=32&&d!=-32&&d!=0)
    那就不匹配

    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;

    int dif = 'a' - 'A';
    char a[1000003];
    char word[11];
    bool has[129];
    int size;
    int len;
    int main(){
    cin >> word;
    gets(a);
    gets(a);
    int i, j;
    int pos = -1;
    int count = 0;
    for (i = 0; a[i]; i++);
    a[i] = ' ';
    a[i + 1] = 0;
    i = 0;

    while (1){
    while (a[i] == ' ')i++;
    if (a[i] == 0)break;
    for (j = 0; word[j]; j++){
    int d = word[j] - a[i + j];
    if (d!=dif&&d!=-dif&&d!=0)
    break;
    }
    if (word[j] == 0&&a[i+j]==' '){
    count++;
    if (pos == -1)pos = i;
    i += j;
    }
    else{
    for (i += j; a[i] != ' '; i++);
    }
    }
    if (!~pos)cout << -1 << endl;
    else cout << count << ' ' << pos << endl;
    return 0;
    }

  • -1
    @ 2014-03-28 13:57:34

    今天发现用pos也不会超时代码如下
    var
    s1,s2:ansistring;
    a,i,ans,k:longint;
    first:boolean;
    begin
    readln(s1);
    readln(s2);
    s1:=' '+upcase(s1)+' ';
    s2:=' '+upcase(s2)+' ';
    first:=true;
    while pos(s1,s2)>0 do
    begin
    a:=pos(s1,s2);
    delete(s2,1,a+length(s1)-2);
    if first then begin k:=a-1; first:=false; end;
    inc(ans);
    end;
    if ans>0 then writeln(ans,' ',k)
    else writeln('-1');
    end.

  • -1
    @ 2014-02-07 14:16:43

    STL的方法:
    可以先读入到一个string里面,然后利用string.find找到第一个位置
    之后就可以直接利用stringstream秒了
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <cctype>

    using namespace std;

    string data, words, reads;
    stringstream sin;
    int main()
    {
    ios::sync_with_stdio(false);
    getline(cin, words);
    getline(cin, data);

    string::size_type len = words.size(), data_end = data.size(), flag = 0;
    for(string::size_type i = 0; i < len; ++i){
    words[i] = tolower(words[i]);
    }

    string::size_type times = 0, first_pos = 0;
    for(string::size_type i = 0; i < data_end; ++i){
    if(isalpha(data[i])) data[i] = tolower(data[i]);
    }

    //先全部换成小写

    data.push_back(' ');//文章末尾补个空格方便查找

    flag = data.find(words + " ");//先考虑一开头就是需要的单词的情况
    if(flag != 0){ //如果不在开头
    flag = data.find(" " + words + " ") + 1; //前后均有空格查找即可,记得最后结果要算上单词前面补的空格
    }
    first_pos = flag;

    sin.str(data);

    while(sin >> reads){
    if(reads == words) ++times;
    };

    if(times != 0) cout << times << " " << first_pos << endl;
    else cout << -1 << endl;

    return 0;
    }

  • -2
    @ 2017-09-24 08:13:36

    so water
    #include<iostream>
    #include<stdio.h>
    using namespace std;
    int main()
    {
    int a[1001],n,i,j,ans=0;
    for(i=1;i<=1000;i++)
    a[i]=0;
    cin>>n;
    for(i=1;i<=n;i++)
    {
    cin>>j;
    a[j]=1;
    }
    for(i=1;i<=1000;i++)
    {
    if(a[i]==1)
    ans++;
    }
    cout<<ans<<endl;
    for(i=1;i<=1000;i++)
    {
    if(a[i]==1)
    cout<<i<<" ";
    }
    return 0;
    }

  • -3
    @ 2017-08-28 17:10:25

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int main(){
    int t=0,tt=0;
    char s[1000001],ss[11];
    gets(ss);
    gets(s);
    for(int i=0;i<=strlen(s)-strlen(ss);++i)
    {
    int j;
    for (j=0;j<=strlen(ss)-1;++j)
    {
    if (toupper(s[j+i])!=toupper(ss[j]))
    break;
    if (i>0&&s[i-1]!=' ')
    break;
    }
    if(j==strlen(ss)&&(s[j+i]==' '||j+i==strlen(s)))
    {
    t++;
    if(t==1)
    tt=i;
    }
    }
    if(t==0)
    printf("-1");
    else
    printf("%d %d\n",t,tt);
    return 0;
    }

信息

ID
1761
难度
7
分类
模拟 | 字符串 点击显示
标签
递交数
2796
已通过
551
通过率
20%
被复制
20
上传者