1.2 Name That Number

本文介绍了一种使用哈希表优化电话号码转换为可能的英文单词的问题解决方法。通过预处理字典文件并建立哈希表,实现快速查询对应数字串的所有有效单词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     这是一个类似于查字典的问题。已知一本“字典”中包含了一些英文单词(5000个左右,单词长度为1~12位),又已知一种对应关系,数字2~9共对应除去Q和Z的24个英文字母,每个数字可对应3个英文字母,如2可对应A,B,C,也就是说A,B,C均可用2表示。那给你一个n位的数字,这个数字可以表示的单词就有3^n种。现题目给定一个1~12位长的数字,要求输出在字典中存在的这个数字所对应成的所有单词。

     由于字典中包含的单词数量不多,我们可以先将单词读入内存,然后一一比较。但这种做法肯定不是最好的,如果单词的个数很多的话,那肯定花的时间会增大,这种方法必须把每个字典里的每个单词都比较一次。

      想到用哈希查找的方法,可以以数字为关键字key,f=key%5000作为哈希函数,定义数组arr[5000],将关键字为key的单词全都存到以arr[f]为头结点的链表中,这样的话每次查询都可以直接定址到key%5000,大大减少查询时间。写了好久,最后终于通过了。

 

/*
ID: whutzha1  
PROG: namenum  
LANG: C++  
*/
#include<fstream>
using namespace std;
ifstream cin("namenum.in");
ifstream fin("dict.txt");
ofstream cout("namenum.out");

struct Node
{
  char name[13];
  long long num;
  Node *next;
};

int main()
{
   Node *arr[5000];
   int i;
   for (i=0;i<5000;i++)
   {
     arr[i]=NULL;
   }
   //读dict.txt文件
   char read_name[13];
   long long read_num;
   int   area;
   int   n;
   char ch;
   while(fin.get(ch))
   {
   for (i=0;i<13;i++)
   {
     while(ch==' '||ch=='\n')
        { fin.get(ch); }
     read_name[i]=ch;
     fin.get(ch);
     if (ch==' '||ch=='\n')
         break;    
   }
   read_name[i+1]='\0';
   read_num=0;
   for(i=0;i<12;i++)
   {
	 switch(read_name[i])
	 {
	    case 'A':
	    case 'B':
	    case 'C':  n=2;break;
	    case 'D':
		case 'E':
		case 'F':  n=3;break;
		case 'G':
		case 'H':
		case 'I':  n=4;break;
		case 'J':
		case 'K':
		case 'L':  n=5;break;
		case 'M':
		case 'N':
		case 'O':  n=6;break;
		case 'P':
		case 'R':
		case 'S':  n=7;break;
		case 'T':
        case 'U':
		case 'V':  n=8;break;
		case 'W':
		case 'X':
		case 'Y':  n=9;break;
		default: break;
	 }
     read_num=(read_num*10+n);
	 if (i==11||read_name[i+1]=='\0') 
	 {
         area=read_num%5000;
         Node *p=(Node *)malloc(sizeof(Node));
		 if (arr[area]==NULL)  
		 {
		   arr[area]=p;
		 }
		 else 
		 {
		   Node *q=arr[area];
		   Node *r;
		   while (q)
		   {
			 r=q;
		     q=q->next;
		   }
		   r->next=p;
		 }
		 p->num=read_num;
		 p->next=NULL;
		 for (i=0;i<13;i++)
		 {
		   p->name[i]=read_name[i];
		   if (p->name[i]=='\0')
		   {
		      break;
		   }
		}
		 break;
	 }
   }  
   }//while 
   long long num;
   bool flag;
   cin>>num;
   Node *p;
   p=arr[num%5000];
   flag=false;
   while(p)
   {
	  if (p->num==num)
	  {
		 flag=true;
	    // for(i=0;i<12;i++)
		// {
		//   cout<<p->name[i];
		//   if (p->name[i+1]=='\0')
		//   {cout<<endl;break;} 
		// }
		cout<<p->name<<endl;
	  }
	  p=p->next;
   }
   if (!flag)  {cout<<"NONE"<<endl;}
   return 0;
}

转载于:https://my.oschina.net/u/585691/blog/71996

分析一下yaml文件格式问题:name: tpub_web description: tpub web # The following line prevents the package from being accidentally published to # pub.dev using `flutter pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 # followed by an optional build number separated by a +. # Both the version and the builder number may be overridden in flutter # build by specifying --build-name and --build-number, respectively. # In Android, build-name is used as versionName while build-number used as versionCode. # Read more about Android versioning at https://developer.android.com/studio/publish/versioning # In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. version: 1.0.0+1 environment: sdk: '>=2.19.6 <3.0.0' # Dependencies specify other packages that your package needs in order to work. # To automatically upgrade your package dependencies to the latest versions # consider running `flutter pub upgrade --major-versions`. Alternatively, # dependencies can be manually updated by changing the version numbers below to # the latest version available on pub.dev. To see which dependencies have newer # versions available, run `flutter pub outdated`. dependencies: flutter: sdk: flutter flutter_svg: 1.1.6 trouter: git: url: https://git.woa.com/flutter_components/TRouterGroup/TRouter.git path: trouter ref: master3+ dependency_overrides: trouter_platform_interface: git: url: https://git.woa.com/flutter_components/TRouterGroup/TRouter.git path: trouter_platform_interface ref: master3+ trouter_web: git: url: https://git.woa.com/flutter_components/TRouterGroup/TRouter.git path: trouter_web ref: master3+ # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 dev_dependencies: flutter_test: sdk: flutter # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is # activated in the `analysis_options.yaml` file located at the root of your # package. See that file for information about deactivating specific lint # rules and activating additional ones. flutter_lints: ^2.0.0 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec # The following section is specific to Flutter packages. flutter: # The following line ensures that the Material Icons font is # included with your application, so that you can use the icons in # the material Icons class. uses-material-design: true assets: - images/ # To add assets to your application, add an assets section, like this: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware # For details regarding adding assets from package dependencies, see # https://flutter.dev/assets-and-images/#from-packages # To add custom fonts to your application, add a fonts section here, # in this "flutter" section. Each entry in this list should have a # "family" key with the font family name, and a "fonts" key with a # list giving the asset and other descriptors for the font. For # example: # fonts: # - family: Schyler # fonts: # - asset: fonts/Schyler-Regular.ttf # - asset: fonts/Schyler-Italic.ttf # style: italic # - family: Trajan Pro # fonts: # - asset: fonts/TrajanPro.ttf # - asset: fonts/TrajanPro_Bold.ttf # weight: 700 # # For details regarding fonts from package dependencies, # see https://flutter.dev/custom-fonts/#from-packages
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值