1 solutions

  • 0
    @ 2024-12-10 23:09:38

    C :

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define maxn 100000
    
    struct node{
    int child[28];
    int len;
    char value;
    }A[maxn];
    
    int ans;
    
    void proc(int index, char s[], int n){
    if(n>=strlen(s))return;
    int i;
    for(i = 0;i < A[index].len; i++){
        if(A[A[index].child[i]].value == s[n])break;
    }
    if(i>=A[index].len){
        A[++ans].value=s[n];
        A[index].child[A[index].len++]=ans;
    }
    proc(A[index].child[i],s,n+1);
    }
    
    int main()
    {
        char s[70];
        ans = 1;
        memset(A,0,sizeof(A));
        while(scanf("%s",s) != EOF) {
            proc(1,s,0);
        }
        printf("%d\n",ans);
        return 0;
    }
    

    C++ :

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    struct re{
    	bool got;
    	int next[30];
    } t[26000];
    char s[100];
    int now,size=0;
    int main(){
    	char ch;
    	while (cin>>s){
    		now=0;
    		for(int i=0;i<strlen(s);++i)
    		if (t[now].next[s[i]-'A']!=0) now=t[now].next[s[i]-'A'];
    		else
    		{
    			size++;
    			t[size].got=false;
    			memset(t[size].next,0,sizeof(t[size].next));
    			t[now].next[s[i]-'A']=size;
    			now=size;
    		}
    		t[now].got=true;
    	}
    	printf("%d",size+1);
    	return 0;
    }
    

    Pascal :

    program shuc;
    var  a:array[1..6699]of string;
         i,j,k,t,s:longint;
    
    procedure qsort(l,r:longint);
    var
      i,j:longint;
      ch,k:string;
    begin
      i:=l;
      j:=r;
      k:=a[(i+j)div 2];
      while i<j do
      begin
      while (a[i]<k)and(i<r) do inc(i);
      while (a[j]>k)and(j>l) do dec(j);
      if i<=j then
        begin
          ch:=a[i];
          a[i]:=a[j];
          a[j]:=ch;
          inc(i);
          dec(j);
        end;
      end;
      if i<r then qsort(i,r);
      if j>l then qsort(l,j);
    end;
    
    procedure init;
    begin
      while not eof do
        begin
          inc(t);
          readln(a[t]);
        end;
      qsort(1,t);
    end;
    
    procedure zuocha;
    begin
      s:=length(a[1]);
      for i:=2 to t do
        begin
          k:=length(a[i]);
          for j:=1 to k do
            if (a[i][j]<>a[i-1][j])or(j>length(a[i-1])) then
            begin
            s:=s+1+k-j;
            break;
            end;
        end;
      writeln(s+1);
    end;
    begin
      init;
      zuocha;
    end.
    
    
    • 1

    Information

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