Uva1262

博主编程时遇到WA问题,原因是每列可能有重复字母;还曾出现TLE,是输出间忘打空格。提到刘汝佳的代码很优美,以暴力方式运行0ms,推测是数据小所致。

WA了半天…结果是因为每一列中可能有重复的字母

一开还是是TLE,查了下发现是输出之间忘了打空格了。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<limits.h>
#include<cmath>
using namespace std;

int tc;
int k;
bool showup[26];
string ch1[7];
string ch2[7];
vector<int> alp[6]; //5列
int d[5];

void init();
void build();
void cal();

int main()
{
     //freopen("1262in.txt","r",stdin);
    cin>>tc;
    while(tc--)
    {
        init();
        build();
        cal();
    }

    return 0;
}

void cal()
{
    int ans=0;
    int col=0;
    while(col<5)
    {
        for(int j=0;j<alp[col].size();j++)
        {
            if(ans+d[col]>=k)
            {
                cout<<char(alp[col][j]+'A');
                col++;
                break;
            }
            ans+=d[col];
        }
        if (ans < k && col == 0)
        {
            cout << "NO" ;
            break;
        }
    }
    cout<< endl;
}

void init()
{
    memset(alp,0,sizeof(alp));
    // cin>>k;
    scanf("%d",&k);
    // cout<<k<<endl;
    for(int i=1;i<=6;i++)
        cin>>ch1[i];

    for(int i=1;i<=6;i++)
        cin>>ch2[i];

        for(int j=0;j<5;j++)
        {
            memset(showup,0,sizeof(showup));
            for (int i = 1; i <= 6; i++)        //ch1第j列上的第i行的每个数都要在ch2的第j列的第k个数进行比较
            {
                for (int k = 1; k <= 6; k++)
                {
                    if (ch1[i][j] == ch2[k][j])
                    {
                        showup[ch1[i][j]-'A']=true;
                        // alp[j].push_back(ch1[i][j]-'A');
                        break;
                    }
                }
            }

            for(int i=0;i<26;i++)
                if(showup[i])
                    alp[j].push_back(i);
            sort(alp[j].begin(), alp[j].end());
        }


}   

//构建每一层的情况  O(5)
void build()
{
    // system("pause");
    for(int i=4;i>=0;i--)
    {
        // d[i]=1;
        d[i]=i==4?1:alp[i+1].size()*d[i+1];
    }

    //chcek
    /*
        cout<<"************************"<<endl;
    for(int i=0;i<5;i++)
        cout<<d[i]<<"\t";
        */

}

刘汝佳是暴力的 ,也是0ms,估摸着是因为数据太小了?(code还是一如既往得优美)

// UVa1262 Password
// Rujia Liu
#include<cstdio>
#include<cstring>
using namespace std;

int k, cnt;
char p[2][6][9], ans[9];

// return true if already found
bool dfs(int col) {
  if(col == 5) {
    if(++cnt == k) {
      ans[col] = '\0';
      printf("%s\n", ans);
      return true;
    }
    return false;
  }
  bool vis[2][26];
  memset(vis, 0, sizeof(vis));
  for(int i = 0; i < 2; i++)
    for(int j = 0; j < 6; j++)
      vis[i][p[i][j][col] - 'A'] = true;
  for(int i = 0; i < 26; i++)
    if(vis[0][i] && vis[1][i]) {
      ans[col] = 'A' + i;
      if(dfs(col+1)) return true;
    }
  return false;
}

int main() {
  int T;
  scanf("%d", &T);
  while(T--) {
    scanf("%d", &k);
    for(int i = 0; i < 2; i++)
      for(int j = 0; j < 6; j++)
        scanf("%s", p[i][j]);
    cnt = 0;
    if(!dfs(0)) printf("NO\n");
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值