http://dsa.openjudge.cn/string/0407/
直接贴代码,注释说明的很详细。
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
int n, case_num = 1;
string str, tmp;
char line[210];
int main()
{
//freopen("in.txt", "r", stdin);
while(cin.getline(line, 210))
{
int len = strlen(line);
for(int i=0;i<len;i++)
str += line[i];
str += '\n';
}
int left = 0, right = 0;
while(true)
{
left = str.find("/*", right);
if(left == -1)
break;
right = str.find("*/", left);
if(right == -1)
break;
// 统计注释符号之前出现过的引号的个数。若为偶数,说明该注释不在字符串内。同时判断转移符比如 "a\"/*ccc*/" 的情况
int yin_num = 0, yin_it = 0;
while(true)
{
yin_it = str.find("\"", yin_it);
if(yin_it==-1 || yin_it>left)
break;
if(yin_it>0 && str[yin_it-1]!='\\')
yin_num++;
yin_it++;
}
if(yin_num%2 == 0)
str = str.substr(0, left) + str.substr(right+2, str.length()-right-2);
right = left+1;
}
cout<<str;
return 0;
}
顺便贴上我用来测试的输入数据。把这个输入数据过了应该可以AC。
#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");
}
"/**/"
"a\"/*ccc*/"