zoukankan      html  css  js  c++  java
  • 单词查找树

    单词查找树

    分析:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string>
     4 using namespace std;
     5 int i,j,n,t,k;
     6 string a[8001];//数组可以达到32*1024=32768,一个单词一个字节 ,一个换行符2个字节
     7 string s;
     8 int main()
     9 {
    10     //freopen("word.txt","r",stdin);
    11     //freopen("word.out","w",stdout);
    12     while(cin>>a[++n]);//读入文件中的单词并存储在数组a中
    13     /*
    14     while(cin>>s)
    15     {
    16         a[++n]=s;
    17     }
    18     */
    19     n--;
    20     for(i=1;i<n;i++)//单词从小到大排序,选排可以改为快排sort(a+1,a+1+n)
    21     {
    22         for(j=i+1;j<=n;j++)
    23         {
    24             if(a[i]>a[j])//两个单词进行交换 
    25             {
    26                 s=a[i];
    27                 a[i]=a[j];
    28                 a[j]=s;
    29             }
    30         }
    31     } 
    32     //cout<<n<<endl;
    33     //for(i=1;i<=n;i++) cout<<a[i]<<endl;
    34         t=a[1].length();//先累加第一个单词的长度
    35         for(i=2;i<=n;i++)//依次计算每个单词对前一个单词的差 
    36         {
    37             j=0;
    38             while(a[i][j]==a[i-1][j]&&j<a[i-1].length())
    39                 j++;//求两个单词相同部分的长度
    40             t+=a[i].length()-j;//累加两个单词的差length(a[i])-j 
    41         } 
    42         cout<<t+1<<endl;
    43         return 0;    
    44 } 
    45  

     

  • 相关阅读:
    幻灯片效果
    国外空间乱码的解决方法
    图片自动适应
    css圆角效
    iframe自适应兼容
    css圆角
    图片自动适应2
    JQuery实现智能输入提示(仿机票预订网站)
    AppDiag类
    c# 渐变算法
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/7425514.html
Copyright © 2011-2022 走看看