第四次练习赛本来想有点创新,结果还是很不幸地做得不够好。
A. AString.h(I)
判断并输出所有是回文串的字符串,唯一要注意的仅仅是分割字符串。数据量其实很小。
//by trashLHC
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<fstream>
#define MAX 101
using namespace std;
bool isSymmetric(string s){
cout<<s<<endl;
for(int i=0;i<s.length()/2;i++)
if(s[i]!=s[s.length()-i-1]&&s[i]!=s[s.length()-i-1]-32&&s[i]!=s[s.length()-i-1]+32)
return false;
return true;
}
string solve(string s){
int place[MAX],top=0;
place[top++]=-1;
for(int i=0;i<s.length();i++)
if(s[i]==' ')
place[top++]=i;
place[top++]=s.length();
for(int i=top-1;i>0;i--){
if(!isSymmetric(s.substr(place[i-1]+1,place[i]-place[i-1]-1))){
s.erase(place[i-1]+1,place[i]-place[i-1]);
}
}
return s;
}
int main(){
//ifstream infile("in.txt",ios::in);
//ofstream outfile("out.txt",ios::out);
int n;
infile>>n;
infile.get();
while(n--){
string s;
getline(infile,s);
outfile<<solve(s)<<endl;
}
}
//by wjfwzzc
#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;
char str[105];
bool isPalin(char *s)
{
int l=strlen(s);
for(int i=0; i<(l>>1); ++i)
if(tolower(s[i])!=tolower(s[l-i-1]))
return false;
return true;
}
int main()
{
int n;
scanf("%d",&n);
getchar();
while(n--)
{
gets(str);
char *sub=strtok(str," ");
while(sub)
{
if(isPalin(sub))
printf("%s ",sub);
sub=strtok(NULL," ");
}
putchar('\n');
}
}
B. AString.h(II)
这道题更简单了,匹配字符串并删除,数据量同样很小。用string类自带函数就可以轻易解决。
#include<iostream>
#include<string>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string s,t;
while(cin>>s)
{
cin.get();
getline(cin,t);
int k=t.find(s);
for(int i=t.find(s); i!=t.npos; i=t.find(s))
t.erase(i,s.size());
cout<<t<<endl;
}
}
C. MH370的黑匣子
就算它题干变得再花,也不过就是个迷宫。只判断是否存在路径,不要求路径最短,所以dfs即可;bfs也可以不过稍微难写一点点。
以下分别放出两个版本,先是dfs的。
#include<cstdio>
#include<cstring>
using namespace std;
const int dx[]= {-1,0,1,0},dy[]= {0,1,0,-1};
char g[15][15];
bool dfs(int x,int y)
{
g[x][y]='#';
for(int i=0; i<4; ++i)
{
int tx=x+dx[i],ty=y+dy[i];
if(g[tx][ty]=='B'||(g[tx][ty]=='*'&&dfs(tx,ty)))
return true;
}
return false;
}
int main()
{
int n,m,sx,sy;
while(~scanf("%d%d",&n,&m))
{
memset(g,'#',sizeof(g))