您的位置:首页 > 其它

【字符串处理】poj1035

2015-04-22 10:18 211 查看
本身是个一眼能看出算法的水题,但是实现还是出了很多问题。

一开始枚举的太奔放。。直接T掉

然后脑抽在了一个地方:其实只要在第一个不同的位置添加或者删除就可以

最近的状态也是非常差,水题都做的艰辛,insert和delete借鉴了小优前辈的写法。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
/*先check正确性,然后考虑增加,删减,和变换一个字符*/
char s[4][50010][51];
int tot[4];
bool flag[50010];
char std_ch[51]="#";
using namespace std;
void Input(int x)
{
for (;;)
{
tot[x]++;
scanf("%s",s[x][tot[x]]);
if (strcmp(s[x][tot[x]],std_ch)==0) break;
}
tot[x]--;
}
void work_change(int x,int y)
{
int len1=strlen(s[2][x]);
int len2=strlen(s[1][y]);
int tmp=0;
for (int i=0;i<max(len1,len2);i++)
if (s[1][y][i]!=s[2][x][i])
{
tmp++;
if (tmp>1) return;
}
flag[y]=true;
}
void work_insert(int x,int y)
{
int len1=strlen(s[2][x]);
int len2=strlen(s[1][y]);
if (len2-len1!=1) return;
int head1=0,head2=0,tot_tmp=0;
while (head1<len1&&head2<len2)
{
if (s[2][x][head1]!=s[1][y][head2])
{
head2++;
tot_tmp++;
if (tot_tmp>1) return;
}
else
{
head1++,head2++;
}
}
flag[y]=true;
}
void work_delete(int x,int y)
{
int len1=strlen(s[1][y]);
int len2=strlen(s[2][x]);
int head1=0,head2=0,tot_tmp=0;
if (len2-len1!=1) return;
while (head1<len1&&head2<len2)
{
if (s[1][y][head1]!=s[2][x][head2])
{
head2++;
tot_tmp++;
if (tot_tmp>1) return;
}
else
{
head1++;
head2++;
}
}
flag[y]=true;
}
bool check(int x)
{
for (int i=1;i<=tot[1];i++)
if (strcmp(s[1][i],s[2][x])==0)
return true;
return false;
}
int main()
{
freopen("poj1035.in","r",stdin);
freopen("poj1035.out","w",stdout);
memset(flag,0,sizeof(flag));
Input(1);
Input(2);
for (int i=1;i<=tot[2];i++)
{
if (check(i))
{
memset(flag,0,sizeof(flag));
printf("%s is correct\n",s[2][i]);
continue;
}
else
{
int len1,len2;
printf("%s:",s[2][i]);
for (int j=1;j<=tot[1];j++)
{
work_delete(i,j);
if (flag[j]==true)
{
printf(" %s",s[1][j]);
flag[j]=false;
continue;
}
work_insert(i,j);
if (flag[j]==true)
{
printf(" %s",s[1][j]);
flag[j]=false;
continue;
}
work_change(i,j);
if (flag[j]==true)
{
printf(" %s",s[1][j]);
flag[j]=false;
continue;
}
}
if (i<tot[2]) printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: