1 条题解

  • 0
    @ 2021-6-15 1:40:08

    C :

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    char* strlwr1(char* str)
    {
        char* str2 = str;
        while (*str2 != '\0')
        {
            if( *str2 >= 'A' && *str2 <= 'Z')
                *str2 += 0x20;
            str2++;
        }
        return str;
    }
     
    char* my_strrstr(char* s1, char* s2)
    {
        char* last;
        char* current;
        last = NULL;
        int count = 0, pos;
        if (*s2 != '\0')
        {
            current = strstr(s1, s2);
            while (current != NULL)
            {
                last = current;
                current = strstr(last + 1, s2);
                count++;
                if (count == 1)pos = last - s1;
            }
        }
        if (last != NULL) printf("%d %d", count, pos);
        else printf("-1");
        return last;
    }
    int main()
    {
        char *s1 = (char *)malloc(10000000);
        if (s1 == NULL)return 0;
        s1[0] = ' ';
        char s2[20] = " ";
        gets(s2 + 1);
        gets(s1 + 1);
        strcat(s1, " ");
        strcat(s2, " ");
        strlwr1(s1);
        strlwr1(s2);
        my_strrstr(s1, s2);
        free(s1);
    }
    

    C++ :

    //ac程序
    #include <iostream>
    #include<string.h>
    using namespace std;
    string a,b;
    int main()
    {
     //freopen("stat.in","r",stdin);
     //freopen("stat.out","w",stdout);
     int x=-1,y=0;//x表示第一次出现的位置,y统计总次数
     getline(cin,a);
     getline(cin,b);
     int la=a.length(),lb=b.length();//或者用a.size()
     int i=0;
     while(i<lb)
     {
      while(b[i]==' '&&i<lb)i++;
      int j=0;
      while(j<la&&i<lb)
      {
       if(a[j]==b[i]||abs(a[j]-b[i])==32)
       {
        i++;j++;
       }
       else
        break;
      }
      if(j==la&&(b[i]==' '||i==lb-1))
      {
       if(x==-1)
        x=i-la;
       y++;
      }
      while(b[i]!=' '&&i<lb)i++;
     }
     if(x==-1)
      cout<<x<<endl;
     else
      cout<<y<<' '<<x<<endl; 
     //while(1);
     return 0;
    }
    

    Pascal :

    var s,v,v1,s1,s2:ansistring;i,ans,x:longint;
    begin
     readln(v);
     v1:=upcase(v);
     readln(s);
     s1:=upcase(s)+' ';
     x:=0;s2:='';ans:=0;
     for i:=1 to length(s1) do
      begin
    
       if s1[i]=' ' then begin
                               if s2=v1 then begin inc(ans);if (ans=1) then x:=i-length(v)-1;end;
                               s2:='';continue;
                         end
        else s2:=s2+s1[i];
      end;
     if ans=0 then writeln(-1) else begin write(ans,' ');writeln(x);end;
    end.
    

    Java :

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String args[]) throws Exception {
    
    		// 输入单词
    		Scanner scanner = new Scanner(System.in);
    		String word = scanner.nextLine();
    		List<String> wordList = str2List(word);
    
    		// 输入句子
    		String sentence = scanner.nextLine();
    		String[] array = sentence.split(" ");
    		List<String> sentenceList = new ArrayList<String>();
    		for (int i = 0; i < array.length; i++) {
    			sentenceList.add(array[i]);
    		}
    
    		// 输出次数、位置
    		fun(wordList, sentenceList, sentence);
    	}
    
    	public static void fun(List<String> wordList, List<String> sentenceList,
    			String sentence) {
    
    		int count = 0;
    		int first = sentence.length();
    		for (int i = 0; i < sentenceList.size(); i++) {
    			List<String> doubtWordList = str2List(sentenceList.get(i));
    			if (match(wordList, doubtWordList)) {
    				count++;
    				int location = 0;
    				for (int j = 0; j < i; j++) {
    					location += (sentenceList.get(j).length() + 1);
    				}
    				if (location < first) {
    					first = location;
    				}
    			}
    		}
    
    		if (count > 0) {
    			System.out.println(count + " " + first);
    		} else {
    			System.out.println("-1");
    		}
    	}
    
    	public static boolean match(List<String> wordList,
    			List<String> doubtWordList) {
    		if (doubtWordList.size() != wordList.size()) {
    			return false;
    		}
    		boolean match = true;
    		for (int i = 0; i < wordList.size(); i++) {
    			String wordU = wordList.get(i).toUpperCase();
    			String wordL = wordList.get(i).toLowerCase();
    			if (!doubtWordList.get(i).equals(wordU)
    					&& !doubtWordList.get(i).equals(wordL)) {
    				match = false;
    				break;
    			}
    		}
    		return match;
    	}
    
    	public static List<String> str2List(String str) {
    		List<String> retList = new ArrayList<String>();
    		if (str == null || str.length() == 0) {
    			return retList;
    		}
    		for (int i = 0; i < str.length(); i++) {
    			retList.add(str.substring(i, i + 1));
    		}
    		return retList;
    	}
    }
    
    

    Python :

    # coding=utf-8
    def main():
        word, sentence = input().lower(), input().lower()
    
        pos = cnt = i = 0
        while i < len(sentence):
            if i == 0 or sentence[i-1] == ' ':
                j = 0
                while j < len(word) and word[j] == sentence[i]:
                    i += 1
                    j += 1
                if j == len(word) and (sentence[i] == ' ' or i == len(sentence)):
                    if cnt == 0:
                        pos = i - len(word)
                    cnt += 1
            i += 1
    
        if cnt > 0:
            print(' '.join([str(cnt), str(pos)]))
        else:
            print(-1)
    
    if __name__ == '__main__':
        main()
    
    • 1

    信息

    ID
    285
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者