簡単なC++テスト問題、練習手ing、間違いを求めます!


皆さんはc言語で主IDとベストを判断するプログラムを書いてください.
タイトル:
1.入力フォーマット偶数n(0<=n<=20)を入力し、n行の情報が続く.各行は2つの文字列からなる.BBS_ID IP_Address BBS_IDは小文字のみで、長さは12を超えず、IP_AddressはこのIDのIPアドレスであり、フォーマットは「A.B.C.D」であり、A,B,C,Dは0−255の範囲の整数である.各IPアドレスはちょうど2つのBBSに対応している.IDは、初登場のIDがメインIDで、後に登場するのがベストです.プログラム入力nが0の場合は終了を示す.2.出力フォーマット各試験例についてn/2行を出力し、出力要求はmain_ID昇順配列.各テスト・インスタンスの後に空の行を出力する必要があります.例えば8 inkfish 192.168と入力.29.24 zhi 192.168.29.235 magicpig 192.168.50.170 pegasus 192.168.29.235 iamcs 202.116.77.131 finalBob 192.168.29.24 tomek 202.116.77.131 magicduck 192.168.50.170 4 mmmmmm 172.16.72.126 kkkkkk 192.168.49.161 llllll 192.168.49.161 nnnnnn 172.16.72.126出力tomek is the MaJia of iamcs finalBob is the MaJia of inkfish magicduck is the MaJia of magicpig pegasus is the MaJia of zhi llllll is the MaJia of kkkkkkknnnnis the MaJia of mmmm
コード(最新バージョンVSで検証&解法で注釈を参照):
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct ID
{
	char main_ID[15];
	char MaJia[15];
	char IP_Address[20];
}AllBBS[22];

/*will be used in sort the strcut array as compare function*/
int cmp(const void *a,const void *b)
{
 struct ID *aa=(struct ID  *)a;
 struct ID *bb=(struct ID  *)b;
 return strcmp(aa->main_ID,bb->main_ID);
}

int main()
{
	/*input redirect to debug\\input.txt file*/
	freopen("debug\\input.txt","r",stdin);

	/* n is the number of BBS_ID, begin the last index of IP info */
	int n,begin=0;
	char tempID[15],tempIP_Address[20];
	while(scanf("%d",&n),n!=0)
	{
		begin=0;
		for(int index=0;index<n;index++)
		{
			scanf("%s %s",tempID,tempIP_Address);
			int findedIP=-1;

			/*fist search the existing array , try to find the esixting IP*/
			for(int i=0;i<begin;i++)
			{
				if (strcmp(AllBBS[i].IP_Address,tempIP_Address)==0) // find the existing IP
				{
					findedIP=i;
					strcpy(AllBBS[i].MaJia,tempID);
				}
			}
			/* not find the existing IP , add a new one*/
			if(findedIP==-1)
			{
				strcpy(AllBBS[begin].main_ID,tempID);
				strcpy(AllBBS[begin].IP_Address,tempIP_Address);
				begin++;
			}
		}

		/*sort the array according to Main_ID*/
		qsort(AllBBS,begin,sizeof(AllBBS[0]),cmp);

		for (int i = 0; i < begin; i++)
		{
			printf("%s is the MaJia of %s
",AllBBS[i].MaJia,AllBBS[i].main_ID); } printf("
"); } return 0; }

キーワード検索
タイトル:
私たちのかわいい新しいウェブサイトは新しい検索機能を持っていて、2つのワイルドカード「*」と「?」を使っています.「*」は0または複数の小文字を表し、「?」1文字を表します.キーワードを入力すると、不確定な場所でワイルドカードを使用します.私たちはデータベースに複数のレコードを持っています.各レコードは小文字で構成されています.今、キーワードを提供します.データベースにキーワードに一致するレコードがいくつあるか教えてくれませんか.たとえば、キーワードがj*y*m*yの場合、ではjiyanmoyu,jyanmoyu,jymyuはいずれも一致する記録である.Inputマルチグループテストデータ、20グループ未満.各テストデータのセットについて、最初の行は入力されたキーワードであり、次はデータベース内のすべてのレコードの数n、1<=n<=10000であり、各レコードの長さは50文字を超えない.Outputは、各テストデータのセットについて、キーワードに一致したい合計レコード数を1行分出力します.  Sample Input  jiyanmoyu 2 jiyanmoyu huyanluanyu ji*moy? 3 jiyanmoyu jimoyu huyanluanyu   Sample Output  1 2
コード(最新バージョンVSテストに成功しました&&解法はコメントを参照):
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

bool map(char *key, int keyI,char *tc, int tcI)
{		
	if(tc[tcI]=='\0')
	{
		if(key[keyI]=='\0' || key[keyI]=='*' )
		{
			return true;		
		}
		return false;		
	}

	switch (key[keyI])
	{
	case '?':
		return map(key,keyI+1,tc,tcI+1); /*one key, one TC*/
	case '*':
		return map(key,keyI+1,tc,tcI) || map(key,keyI,tc,tcI+1)|| map(key,keyI+1,tc,tcI+1);/*0   OR 1+  OR 1 */
	default:
		return (key[keyI]==tc[tcI]) && map(key,keyI+1,tc,tcI+1);
	}

}

int main()
{
	/*input redirect to debug\\input.txt file*/  
	freopen("debug\\input.txt","r",stdin);

	char key[55],tc[55];
	int n=0,total=0;
	while(scanf("%s",key),key[0]!='\0')
	{
		scanf("%d",&n);
		total=0;
		for (int i = 0; i < n; i++)
		{
			scanf("%s",tc);
			if(map(key,0,tc,0)==true)
			{
				//printf("%s
",tc); total++; } } printf("%d
",total); key[0]='\0'; } return 0; }