nkoj 1742(Astar2007)

 

这道题目在nkoj上面只有一组测试数据,所以比较好偷懒,我也偷懒了,所以只处理了只有一张表的查询。

对于有N张表的查询,只需将T定义成数组table T[N]就可以操作了,稍复杂一点,但是框架已经出来了

问题今本已经解决,担心内存和时间的问题,这道题用来训练编程能力很有帮助(仅仅是编程,无关算法)

应该特别注意的是对数组进行操作时,要时刻关注地址(下标),实际上操作十数组时只需要关心地址就可以了

,因为操作数组的实质是操作内存,注意一定不要超出数组边界,不然会发生乱码或者更严重的错误。

嗯,下面是初步代码,可以在nkoj上AC(虽然一组数据没什么价值),以后有时间再补全。

 

#include
#include
#include
using namespace std;
#define max_c 10
#define max_n 10
#define max_t 10
#define max_q 1001
int c, n, q;
typedef struct{
	string IDname;
	string titlename[max_c];
	string content[max_n][max_c];
	int countt;
}table;
table T;
typedef struct{
	string name;
	string content;
}getinfo;
int main()
{
	while (cin >> c >> n >> q){
		//input the one tible
		cin >> T.IDname;
		T.countt =n;
		//input the titename
		for (int i = 0; i < c; i++){
			cin >> T.titlename[i];
		}
		//input the content
		for (int i = 0; i < n; i++){
			for (int j = 0; j < c; j++){
				cin >> T.content[i][j];
			}
		}
		//input done,ask qords input and ask
		char quir[max_q];
		cin.getline(quir, max_q);
		for (int i = 0; i < q; i++){
			cin.getline(quir, max_q);
			int equ = 0;
			for (int k = 0; quir[k] != '\0'; k++){
				if (quir[k] == '='){
					equ++;
				}
			}
			if (equ == 0){
				cout << T.countt << endl;
				continue;
			}
			getinfo s1,s2;
			s1.content = "";
			s1.name = "";
			s2.content = "";
			s2.name = "";
			int ans = 0;
			if (equ == 1){
				for (int k = 0; quir[k] != '\0'; k++){
					if (quir[k] == '='){
						k--;
						for (int h = k; quir[h] != ' ';h--){
							s1.name= quir[h]+s1.name;
						}
						for (; quir[k] != '='; k++);
						k++;
						for (int h = k; quir[h] != ' '&&quir[h] != ')'&&quir[h]!='\0'; h++){
							s1.content+= quir[h];
						}
					}
				}
				int idexname = 0;
				for (int k = 0; k < n; k++){
					if (s1.name == T.titlename[k]){
						idexname = k;
						break;
					}
				}
				for (int k = 0; k < n; k++){
					if (T.content[k][idexname] == s1.content){
						ans++;
					}
				}
				cout << ans << endl;
				continue;
			}
			if (equ == 2){
					for (int k = 0; quir[k] != '\0'; k++){
					if (quir[k] == '='){
						if (s1.content== ""&&s1.name==""){
							k--;
							for (int y = k; quir[y] != ' '; y--){
								s1.name = quir[y]+s1.name;
							}
							for (; quir[k] != '='; k++);
							k ++;
							for (int y = k; quir[y] != ' '&&quir[y] != ')'&&quir[y]!='\0'; y++){
								s1.content+= quir[y];
							}
						}
						else{
							k--;
							for (int y = k; quir[y] != ' '; y--){
								s2.name = quir[y] + s2.name;
							}
							for (; quir[k] != '='; k++);
							k++;
							for (int y = k; quir[y] != ' '&&quir[y] != ')'&&quir[y]!='\0'; y++){
								s2.content += quir[y];
							}
						}
					}
				}
				int idexname1 = -1, idexname2 = -1;
				for (int a = 0; a < n; a++){
					if (idexname1 != -1 && idexname2 != -1){
						break;
					}
					if (T.titlename[a] == s1.name){
						idexname1 = a;
					}
					if (T.titlename[a] == s2.name){
						idexname2 = a;
					}
				}
				for (int b = 0; b < n; b++){
					if (T.content[b][idexname1] == s1.content&&T.content[b][idexname2] == s2.content){
						ans++;
					}
				}
				cout << ans << endl;
				continue;
			}	}
	}
	return 0;
}


 

 

你可能感兴趣的:(ACM,nkoj,C++,Astar)