UVA Solution 146 - ID Codes - Solution in C++ | Volume 1 - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Saturday, April 29, 2017

UVA Solution 146 - ID Codes - Solution in C++ | Volume 1

UVA Solution 146 - ID Codes - Solution in C++ | Volume 1


Problem Name: ID Codes
Problem Number : UVA - 146
Online Judge : UVA Online Judge Solution
Volume: 1
Solution Language : C plus plus

UVA Solution 146 Code in CPP:


#include <stdio.h>
#include <string.h>
int cnt[26], flag, len;
char str[100], tmp[100];
void dfs(int idx) {
    if(flag == 2)
        return ;
    if(idx == len) {
        if(flag == 0) {
            flag = 1;
        } else {
            tmp[idx] = '\0';
            puts(tmp);
            flag = 2;
        }
        return ;
    }
    int i;
    for(i = 0; i < 26; i++) {
        if(flag == 0) {
            if(str[idx]-'a' == i) {
                cnt[i]--;
                tmp[idx] = i+'a';
                dfs(idx+1);
                cnt[i]++;
            }
        } else {
            if(cnt[i] > 0) {
                cnt[i]--;
                tmp[idx] = i+'a';
                dfs(idx+1);
                cnt[i]++;
            }
        }
    }
}
int main() {
    int i;
    while(scanf("%s", str) == 1) {
        if(!strcmp("#", str))
            break;
        memset(cnt, 0, sizeof(cnt));
        flag = 0, len = strlen(str);
        for(i = 0; str[i]; i++)
            cnt[str[i]-'a']++;
        dfs(0);
        if(flag != 2)
            puts("No Successor");
    }
    return 0;
}

1 comment:

  1. hi. can u explain how the string is getting successed ? whats the logic behind it

    ReplyDelete