POJ 3548 Restoring the digits

暴力搜索。注意题目说每个字母对应的数字不同,这句话表明最多只有10个字母,所以暴力DFS绝对不会TLE。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

char s[1000],t[1000];
char cun[1000];
int C[1000];
int ans[1000];
int tot;
int flag;
int rt[15];

void DFS(int x)
{
    int i;
    if(x==tot)
    {
        t[0]=‘\0‘;
        strcpy(t,s);
        for(i=0;t[i];i++)
            if(t[i]>=‘A‘&&t[i]<=‘Z‘)
                t[i]=ans[C[t[i]]]+‘0‘;
        int num1=0;
        int num2=0;
        int num3=0;
        int num=0;
        int jia=0;
        int jian=0;
        for(i=0;t[i];i++)
        {
            if(t[i]>=‘0‘&&t[i]<=‘9‘) num=num*10+t[i]-‘0‘;
            if(t[i]==‘+‘) num1=num,num=0,jia=1;
            if(t[i]==‘-‘) num1=num,num=0,jian=1;
            if(t[i]==‘=‘) num2=num,num=0;
        }
        num3=num,num=0;
        if(jia==1)
            if(num1+num2==num3)
                flag=1;
        if(jian==1)
            if(num1-num2==num3)
                flag=1;
        return;
    }
    for(i=0;i<=9;i++)
    {
        if(rt[i]==0)
        {
            ans[x]=i;
            rt[i]=1;
            DFS(x+1);
            if(flag) return;
            rt[i]=0;
        }
    }
}

int main()
{
    int i;
    while(~scanf("%s",s))
    {
    memset(C,0,sizeof(C));
    tot=1;flag=0;
    for(i=0;s[i];i++)
    {
        if(s[i]>=‘A‘&&s[i]<=‘Z‘)
        {
            if(C[s[i]]==0)
            {
                C[s[i]]=tot;
                cun[tot]=s[i];
                tot++;
            }
        }
    }
    memset(rt,0,sizeof(rt));
    DFS(1);
    int flag[1000];
    int shuzi[1000];
    memset(flag,0,sizeof(flag));
    for(i=1;i<tot;i++)
    {
        flag[cun[i]]=1;
        shuzi[cun[i]]=ans[i];
    }
    for(i=‘A‘;i<=‘Z‘;i++)
        if(flag[i])
            printf("%c %d\n",i,shuzi[i]);
    }
    return 0;
}
时间: 2024-10-12 18:49:41

POJ 3548 Restoring the digits的相关文章

poj 3548 Restoring the digits(DFS)

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int use[30],a[30],flag,b[30],m,p[20]; char s[1024]; void dfs(int x) { int i,a1,a2,a3,t,l; if(flag) return; if(x==m) { l=strlen(s); a1=0; a2=0; a3=0; for(i=0; i

数据降维 实例

secom.data 3030.93 2564 2187.7333 1411.1265 1.3602 100 97.6133 0.1242 1.5005 0.0162 -0.0034 0.9455 202.4396 0 7.9558 414.871 10.0433 0.968 192.3963 12.519 1.4026 -5419 2916.5 -4043.75 751 0.8955 1.773 3.049 64.2333 2.0222 0.1632 3.5191 83.3971 9.5126

POJ 3373 Changing Digits

题目大意: 给出一个数n,求m,使得m的长度和n相等,能被k整除.有多个数符合条件输出与n在每位数字上改变次数最小的.改变次数相同的输出大小最小的.  共有两种解法:DP解法,记忆化搜索的算法. 以后会更新记忆化搜索. 1.DP解法: 解题思路: DP[i][j]表示数n的前i位除以k余j最小改变几位. DP[len][0]就表示数n被k整除最小改变几位. 根据这个关系从后向前遍历DP数组可以找出所有满足条件的数的路径. 再根据关系从前往后输出.  下面是代码: #include <stdio.

POJ 3373 Changing Digits 好蛋疼的DP

一开始写的高位往低位递推,发现这样有些时候保证不了第四条要求.于是又开始写高位往低位的记忆化搜索,又发现传参什么的蛋疼的要死.然后又发现高位开始的记忆化搜索就是从低位往高位的递推呀,遂过之. dp[i][j]记录在i位 且 余数为j时的最优解情况. dp[i][j].next表示当前的最优解是由哪一种状态转移过来的. 代码又写锉了.. #include <algorithm> #include <iostream> #include <cstring> #include

poj 2720 Last Digits

Last Digits Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2233   Accepted: 474 Description Exponentiation of one integer by another often produces very large results. In this problem, we will compute a function based on repeated expone

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

poj 1151 Atlantis

Atlantis http://poj.org/problem?id=1151 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22662   Accepted: 8478 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts

Poj 1654--Area(叉积)

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17377   Accepted: 4827 Description You are going to compute the area of a special kind of polygon. One vertex of the polygon is the origin of the orthogonal coordinate system. From thi

POJ 2704 Pascal&#39;s Travels (基础记忆化搜索)

Pascal's Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5328   Accepted: 2396 Description An n x n game board is populated with integers, one nonnegative integer per square. The goal is to travel along any legitimate path from t