求调输出格式
查看原帖
求调输出格式
470348
oddy楼主2023/3/13 15:59

如题,我已经把求解过程调得没有问题,但是输出词对时,总会多输出一个回车。举例来说,样例输出是

24
program
prom rag

而我的输出是

24
program
prom
 rag
#include <cstdio>
#include <cstring>

const int a[] = {2, 5, 4, 4, 1, 6, 5, 5, 1, 7, 6, 3, 5, 2,
3, 5, 7, 2, 1, 2, 4, 6, 6, 7, 5, 7};
int n, val, cnt, dnt, c[256], tc[256], ans[5000][2];
char s[8], s1[8], t[40005][8];

int main() {
    freopen("lgame.in", "r", stdin);
    freopen("lgame.out", "w", stdout);
    scanf("%s", s);
    for(int i = 0; s[i]; i++) c[s[i]]++;

    freopen("lgame.dict", "r", stdin);
nxt:while(scanf("%s", s1) && *s1 != '.') {
        memset(tc, 0, sizeof tc), val = 0;
        for(int i = 0; s1[i]; i++) {
            if(++tc[s1[i]] > c[s1[i]]) goto nxt;
            val += a[s1[i]-'a'];
        }

        strcpy(t[++n], s1);
        if(val > dnt) dnt = val, cnt = 0;
        if(val == dnt) ans[++cnt][0] = n;
    }

    for(int i = 1; i <= n; i++)
        for(int j = i; j <= n; j++) {
            memset(tc, 0, sizeof tc), val = 0;
            for(int k = 0; t[i][k]; k++) {
                if(++tc[t[i][k]] > c[t[i][k]]) goto fi;
                val += a[t[i][k]-'a'];
            }
            for(int k = 0; t[j][k]; k++) {
                if(++tc[t[j][k]] > c[t[j][k]]) goto fi;
                val += a[t[j][k]-'a'];
            }
            if(val > dnt) dnt = val, cnt = 0;
            if(val == dnt) ans[++cnt][0] = i, ans[++cnt][1] = j;
    fi:;}
// 从这里开始是输出部分
    printf("%d\n", dnt);
    for(int i = 1; i <= cnt; i++)
        if(ans[i][1]) printf("%s %s\n", t[ans[i][0]], t[ans[i][1]]);
        else puts(t[ans[i][0]]);
}

请问我的问题出在哪里?

2023/3/13 15:59
加载中...