1 solutions

  • 0
    @ 2024-12-20 22:52:18

    C :

    #include<stdio.h>
    #include<string.h>
    void sort_string(char a[])
    {
    	int i,j,len=strlen(a);
    	char t;
    	for(i=0;i<len;i++)
    	{
    		for(j=i+1;j<len;j++)
    		{
    			if(a[i]>a[j])
    			{
    				t=a[i];
    				a[i]=a[j];
    				a[j]=t;
    			}
    		}
    	}
    }
    int main()
    {
    	char mstr[1001],sstr[101],temp[101];
    	while(scanf("%s",mstr)!=EOF)
    	{
    		scanf("%s",sstr);
    		int i,endpos,mlen=strlen(mstr),slen=strlen(sstr);
    		int count=0;
    		sort_string(sstr);
    		//printf("@@%s\n",sstr);
    		for(i=0;i<=mlen-slen;i++)
    		{
    			endpos=i+slen;
    			char t=mstr[endpos];
    			mstr[endpos]='\0';
    			strcpy(temp,&mstr[i]);
    			mstr[endpos]=t;
    			sort_string(temp);
    			//printf("!!%s\n",temp);
    			if(strcmp(temp,sstr)==0)
    				count++;
    		}
    		printf("%d\n",count);
    	}
    	return 0;
    }
    
    

    C++ :

    #include<cstdio>
    #include<string>
    using namespace std;
    
    int main()
    {
    	char a[1001],b[101];
    	string A,B,C,D;
    	int i,j,la,lb,s;
    	while(scanf("%s%s",a,b)!=EOF)
    	{
    		A=a;
    		B=b;
    		la=A.length();
    		lb=B.length();
    		for(s=i=0;i<la;i++)
    		{
    			C=A.substr(i,lb);
    			for(j=0;j<lb;j++)
    			{
    				D=B.substr(j);
    				D.append(B,0,j);
    				if(C==D)
    				{
    					s++;
    					break;
    				}
    			}
    		}
    		printf("%d\n",s);
    	}
    	return 0;
    }
    

    Pascal :

    program p1055;
    var st:ansistring;
        a,b:ansistring;
        la,lb,i,j,k,n:longint;
        f:boolean;
        p:array[1..100] of string;
    function check(s:string):boolean;
    var i:longint;
    begin
     check:=true;
     for i:=1 to k do
     if p[i]=s then exit(false);
    end;
    begin
     while not eof do
       begin
        readln(a);
        readln(b);
        la:=length(a);
        lb:=length(b);
        n:=0; k:=0;
        for i:=1 to lb do
         begin
          f:=check(b);
          st:=a;
          while (pos(b,st)>0) and f do
           begin
            n:=n+1;
            delete(st,1,pos(b,st));
           end;
          k:=k+1;
          p[k]:=b;
          b:=copy(b,2,lb-1)+b[1];
         end;
        writeln(n);
       end;
    end.
    
    

    Java :

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Main
    {
    
        public static void main(String[] args) throws IOException
        {
    	// TODO Auto-generated method stub
    
    	String A, B;
    	int lenA, lenB, len, count,count2, duplicate;
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	A = br.readLine();
    	B = br.readLine();
    	while(true)
    	{
    	    
    	    if (A == null || B == null)
    	    {
    		break;
    	    }
    	    lenA = A.length();
    	    lenB = B.length();
    	    
    	    count = 0;
    	    len = lenA - lenB + 1;
    	    for(int i = 0;i < len;i++)
    	    {
    		count2=0;
    		for(int j = 0;j < lenB;j++)
    		{
    
    		    if (equalsAB(A, i, B, j, lenB))
    		    {
    			duplicate = 0;
    			count2++;
    			for(int k = j + 1;k < lenB;k++)
    			{
    			    if (equalsBB(B, j, B, k, lenB))
    			    {
    				duplicate=1;// 重复的
    				break;
    			    }
    			}
    			count2 -= duplicate ;// 减去重复的
    		    }
    		}
    		count+=count2;
    
    	    }
    	    System.out.println(count);
    	    A = br.readLine();
    	    B = br.readLine();
    	}
        }
    
        static boolean equalsAB(String cs1, int offset1, String cs2, int offset2, int len)
        {
    	for (int i = 0; i < len; i++)
    	{
    		if (cs1.charAt(offset1+i)!=cs2.charAt((offset2+i)%len))
    		{
    			return false;
    		}
    	}
    	return true;
        }
    
        static boolean equalsBB(String cs1, int offset1, String cs2, int offset2, int len)
        {
    	for(int i = 0;i < len;i++)
    	{
    	    if (cs1.charAt((offset1 + i) % len) != cs2.charAt((offset2 + i) % len))
    	    {
    		return false;
    	    }
    	}
    	return true;
        }
    
    }
    
    

    Python :

    #b in a
    def mycount(a,b):
        if a=='' or b=='':
            return 0
        count = 0
        j = 0
        while True:
            if b == a[j:(len(b)+j)]:
                count += 1
            j += 1
            if j + len(b)>len(a):break
        return count
    
    while True:
        A = raw_input()
        B = raw_input()
        lst = []
        sumi = 0
        for i in range(0,len(B)):
            lst.append(B)
            B = B[1:len(B)] + B[0]
        lst2 = list(set(lst))
        #print lst2
        for i in lst2:
            sumi += mycount(A,i)
        print sumi
            
        
    
    
    • 1

    Information

    ID
    789
    Time
    1000ms
    Memory
    32MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By