cLanguage String Question

Asked 2 years ago, Updated 2 years ago, 40 views

#include <stdio.h>
#include <string.h>

int main(void)
{
    char arr[101];
    int i, j, n;
    char ch;
    int num, cnt=0, flag=0;
    scanf("%d", &num);
    for(i=0; i<num; i++)
    {
        scanf("%s", arr);
        if(strlen(arr) == 1)
        {
            cnt++;
            continue;
        }
        for(j=0; j<strlen(arr); j++)
        {
            ch = arr[j];
            for(n=j+1; n<strlen(arr); n++)
            {
                if(ch != arr[n])
                {
                    if(ch == arr[n+1])
                    {
                        flag++;
                        break;
                    }
                }
            }
        }
        if(flag == 0)
            cnt++;
    }
    printf("%d", cnt);

    return 0;
}

The code above is the code that receives the string and counts the group words.

(ccazzzzbb is a group word because c, a, z, and b are all consecutive, and kin is also a group word because k, i, and n are consecutive, but aabbbccb is not a group word because b appears apart.)

The way it works is to take a string and compare each element of the string array. If there is a group word, flag will increase by 1 and cnt will not increase. So it's not a group word, so I don't count. But... It comes out properly, but Baekjun's score is wrong. I think it's coming out well <

string c

2022-09-21 11:12

1 Answers

for(j = 0; j < strlen(arr); j++) {
    ch = arr[j]
    for(n = j+1; n < strlen(arr); n++){
        if(ch != arr[n] && ch == arr[n+1]){
            flag++;
            break;
        }
    }
}

I took only a part of the code you wrote, so I modified it slightly.

If you look at the code, save the first character in the string and check the characters after it, but if you see a character different from the one you saved, check the next character, and if the character is the same as the one you saved, add 1 to the flag.

If aba comes in as an input value, you can confirm that it is not a group word.

But what if abc comes in as an input value?

Since strlen(arr) = 3, consider j = 0, n = 2.

ch!=arr[n] is true because 'a'!='c', but what about ch==arr[n+1]?

In other words, what happens if you call arrr[3] when strlen(arr) = 3?

Also, if you use the input value, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccc

What if somebody comes in?

We explored the entire string to verify that the first character, a, did not appear in a discontinuous position.

By the way, the second text is also a

Again, explore the entire string to make sure that a is not in a discontinuous position.

I think I did it earlier, but I still do it.

I need to check on the third text. It's a again?

But I have to do it again. Until you get any other characters other than a, it's a repetitive task, but it continues.

How and how a has passed.

Now we need to check b.

Using the first b, we checked that there was no discontinuous b after that.

But after that, b, after that, b, b, b...

I did all the b's, and this time, it's c...

I think time will be up before I even explore everything

Once you have the size of the array and you use the for statement to give the index value of the array, it is not recommended to perform additional operations on the values obtained.

If you haven't thought about it, you can be out of the range of the array.

And in the problem of searching for a string, how many times does search for the string to solve the problem?Think about and try to solve the problem within that number of times Haha


2022-09-21 11:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.