18.10.10 数算作业-字符串

题目内容:

老师给小学生门布置了一些作业,让它们按照一个模版写一些字符串交上来,

同学们把作业交上来了,问题来了,这么多的作业老师批改不过来,现在请你帮老师

写一个程序,帮助老师确定各个字符串是否合格。

    首先老师有一个匹配模版,比如是“aa[123]bb”这一个字符串,同学们交的各种

作业字符串如aa1bb、aa2bb、aa3bb都算是正确匹配看,而aacbb就是错误的字符串。

(即待查字符串对应于模版方括号内的部分,应该为方括号内字符串的一个子字符)。

    我们需要做的就是按照模版,找出正确的字符串和所在的行。

 

输入格式:

输入的第一行为一个整数n,表示有多少个学生的作业,即有多少行需要检查的字符串。(1<=n<=50)

中间为n行字符串,代表着n个学生们写的作业。每个字符串长度小于50。

最后一行为1行字符串,代表着老师给的匹配模板。

 

输出格式:

输出合格的字符串的行号和该字符串。(中间以空格隔开)

 

输入样例:

4
Aab
a2B
ab
ABB
a[a2b]b

 

输出样例:

1 Aab
2 a2B
4 ABB
 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 
12 #define lowbit(x) x&(-x)
13 
14 using namespace std;
15 
16 string modu;//模板
17 string hw[55];//同学写的作业
18 int n;
19 
20 bool alphas(char a, char b) {
21     if (a == b)
22         return true;
23     if (a <= 'z' || a >= 'a')
24         if (a + 'A' - 'a' == b)
25             return true;
26     if (a <= 'Z' || a >= 'A')
27         if (a + 'a' - 'A' == b)
28             return true;
29     return false;
30 }
31 
32 bool cmp(string mod, string hw) {
33     int modl = mod.length(), hwl = hw.length(),j=0;
34     for (int i = 0; i < modl;) {
35         if (j >= hwl)//当hw的长度较小时
36             return false;
37         if (alphas(mod[i], hw[j])) {
38             i++,j++;
39             continue;
40         }//当两者无法匹配
41         if (mod[i] == '[') {
42             bool flag = false;
43             while (mod[i] != ']') {
44                 i++;
45                 if (flag)
46                     continue;
47                 if (alphas(mod[i], hw[j]))
48                     flag = true;
49             }
50             i++;
51             if (flag)
52             {
53                 j++;
54                 continue;
55             }
56             else
57                 return false;
58         }//当出现[时
59         else
60             return false;
61     }
62     if (j == hwl)//当两者刚好匹配时,j现在
63         return true;
64     return false;
65 }
66 
67 int main()
68 {
69     scanf("%d\n",&n);
70     for (int i = 1; i <= n; i++)
71         cin >> hw[i];
72     cin >> modu;
73     for (int i = 1; i <= n; i++)
74     {
75         if (cmp(modu, hw[i]))
76             cout << i << " " << hw[i] << endl;
77     }
78     return 0;
79 }
View Code

题目内容:

C程序的注释用/*...*/来表示。请写一个程序,将输入的C程序源代码中的注释去掉,输出去掉注释之后的源代码。

用于测试的C代码保证符合语法,不使用C++的//注释语法。

注意,C语言不允许出现嵌套注释。具体来说,对于/*/**/"*/",如果不允许嵌套注释,那么它表示字符串"*/";如果允许嵌套注释,它表示一个引号"。

还请注意,字符串中出现的注释符/*属于字符串的一部分,注释中出现的双引号"属于注释的一部分。

 

输入格式:

符合语法的C代码文本文件。代码每行不超过200个字符。

 

输出格式:

去掉注释后的C代码。要求只能去掉注释,不可以做其他的修改,比如调整缩进,去除注释之外的换行符等。

 

输入样例:

#include#include#include/*Hash Search: 
Hash function: division method; 
handling collisions: open addressing's linear probing. 
In this exercise, M is the basic area's length, all keys are non negative integers.*/

#define M 11

int hash(int key)
{
	return key % M;
}

void init_hash(int* hashtable)
{
	int i;
	for(i = 0; i < M; ++i)
	{
		hashtable[i] = -1;
	}
}

/*return value: 
1:found, *position is the key's index; 
0:not found, *position is where to insert the key; 
-1:overflow. */
int search_hash(int* hashtable, int key, int* position)
{
	int i, h = hash(key);
	for(i = 0; i < M; ++i)
	{
		if(key == hashtable[h])
		{
			*position = h;
			return 1;
		}
		if(-1 == hashtable[h])
		{
			*position = h;
			return 0;
		}
		h = (h+1) % M;
	}
	*position = -1;
	return -1;
}

/*return value: 1:inserted, 0:overflow*/
int insert_hash(int* hashtable, int key)
{
	int position, result;
	result = search_hash(hashtable, key, &position);
	if(-1 == result)
		return 0;
	hashtable[position] = key;
	return 1;
}

void main()
{
	int hashtable[M];
	init_hash(hashtable);
	srand(time(NULL));
	int i, j, key;
	for(i = 0; i < 8; ++i) 	/*make a hash table with 8 elements*/
	{
		key = rand() % 50;
		insert_hash(hashtable, key);
		printf("Insert %d\n", key);
		for(j = 0; j < M; ++j)
			printf("%3d", hashtable[j]);
		printf("\n");
	}

	printf("Please input the key to search:\n");
	scanf("%d", &key);
	i = search_hash(hashtable, key, &j);
	if(1 == i)
		printf("Found!Its index is %d\n", j);
	else
		printf("Not found!\n");
}

 

输出样例:

#include#include#include#define M 11

int hash(int key)
{
	return key % M;
}

void init_hash(int* hashtable)
{
	int i;
	for(i = 0; i < M; ++i)
	{
		hashtable[i] = -1;
	}
}


int search_hash(int* hashtable, int key, int* position)
{
	int i, h = hash(key);
	for(i = 0; i < M; ++i)
	{
		if(key == hashtable[h])
		{
			*position = h;
			return 1;
		}
		if(-1 == hashtable[h])
		{
			*position = h;
			return 0;
		}
		h = (h+1) % M;
	}
	*position = -1;
	return -1;
}


int insert_hash(int* hashtable, int key)
{
	int position, result;
	result = search_hash(hashtable, key, &position);
	if(-1 == result)
		return 0;
	hashtable[position] = key;
	return 1;
}

void main()
{
	int hashtable[M];
	init_hash(hashtable);
	srand(time(NULL));
	int i, j, key;
	for(i = 0; i < 8; ++i) 	
	{
		key = rand() % 50;
		insert_hash(hashtable, key);
		printf("Insert %d\n", key);
		for(j = 0; j < M; ++j)
			printf("%3d", hashtable[j]);
		printf("\n");
	}

	printf("Please input the key to search:\n");
	scanf("%d", &key);
	i = search_hash(hashtable, key, &j);
	if(1 == i)
		printf("Found!Its index is %d\n", j);
	else
		printf("Not found!\n");
}
 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 #include <fstream>
12 
13 using namespace std;
14 
15 bool innote = false,instring=false;
16 string code;
17 
18 void process(string line) {
19     int l = line.length(),i;
20     for (i = 0; i <= l-1; ) //测试目前字符和下一个字符是否能组成注释符;在合适的条件输出
21     {
22         if (!instring&&!innote && line[i] == '/'&&line[i + 1] == '*') {
23             innote = true;
24             i += 2;
25             continue;
26         }//当遇到左注释符且此时并不在字符串或注释中
27         else if (!instring&&!innote && line[i] == '"') {
28             instring = true;
29             i += 1;
30             cout<<'"';
31             continue;
32         }//当遇到左字符串符且此时并不在字符串或注释中
33         else if (innote&& line[i] == '*'&&line[i + 1] == '/') {
34             innote = false;
35             i += 2;
36             continue;
37         }//当遇到右注释符且此时在注释中
38         else if (instring&&line[i] == '\\'&&line[i + 1] == '"') {
39             cout << line[i]<<line[i+1];
40             i += 2;
41             continue;
42         }//当遇到转义符号时
43         else if (!innote&&instring&&line[i] == '"') {
44             instring = false;
45             i++;
46             cout << '"';
47             continue;
48         }//当遇到右字符串符时
49         else if (!innote)
50             cout << line[i];
51         i++;
52     }
53 }
54 
55 int main()
56 {
57     while (getline(cin, code))
58     {
59         code += "\n";
60         process(code);
61     }
62     return 0;
63 }
View Code

被转义字符坑到

posted @ 2018-10-10 22:40  TobicYAL  阅读(832)  评论(0编辑  收藏  举报