Solution to a problem Luogu P3370

Talk about several approaches this question:

Matching violence

rt, violent match that string together a deposit equal to one sentence

Time complexity $ O (n ^ 2 · m) $

Look at the range of data

$ N \ LE10 ^ 5 m \ ^ 3 $ LE10

The spot explosion.Of course there is violence in points

Code (20pts):

#include <bits/stdc++.h>
using namespace std;
char c[100001][1001];
bool pd(int x, int y)
{
    int l1 = strlen(c[x]), l2 = strlen(c[y]);
    if(l1 != l2) return 0;
    for(int i = 0; i < l1; i++)
    {
        if(c[x][i] != c[y][i]) return 0;
    }
    return 1;
}
int main()
{
    int n;
    cin >> n;
    int ans = n;
    for(int i = 1; i <= n; i++)
    {
        cin >> c[i];
        for(int j = 1; j < i; j++)
        {
            if(pd(i, j)) ans--;
        }
    }
    cout << ans;
    return 0;
}

Haopiaoliangya

Haopiaoliangya

WA Do not ask me why, and I thought transfer

string method

Compare the normal way

The idea is to keep down all the strings, for lexicographically sorting a string carrying the operator (less than or greater)

Then follow lexicographic order is determined whether to repeat

Well time complexity

$ O (n · logn) $, you cancardLive

Code (100pts):

#include <bits/stdc++.h>
using namespace std;
string s[10001];
int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> s[i];
    }
    sort(s + 1, s + n + 1);//因为string自带大于和小于所以不用cmp
    int ans = n;
    for(int i = 1; i < n; i++)
    {
        if(s[i] == s[i + 1]) ans--;
            //若两个字符串相同则他们的字典序一定是相邻的
    }
    cout << ans;
    return 0;
}

C ++ STL law

Of course we can do it with everything STL ~

First to think: we judge whether a digital repetition is what is it?

Course bool used [1001] it

This question is asked to judge how to do string

The string type array subscript inflicted chant

Make the protagonist: map

Simply put, when we define an array, the array can determine the type of number (such as char, bool, int, long long, etc.), and the subscript is a fixed type, i.e. integer

However map can determine if the two types, that is to say, we can even string as a subscript, as a basic digital type to a "counter-array"(Do not ask me what the anti-arrays are literally)

(That is not to write $ a_ {interesting} = 3 $ a)

ThenThe upper bound on the upper sentenceThis problem can do it!

Time complexity unknown Anyway, it wants to lead a

Code (100pts):

#include <bits/stdc++.h>
using namespace std;
map < string , bool > m;//定义一个以string类型为下标的bool数组
string s;
int main()
{
    int n;
    cin >> n;
    int ans = n;
    for(int i = 1; i <= n; i++)
    {
        cin >> s;
        if(m[s]) ans--;
        else m[s] = 1;
    }
    cout << ans;
    return 0;
}

Shorter than violence

HASH method

Do not ask why the final, which game you've ever seen you make a start hit the BOSS?

The C ++ STL law and have the same purpose Said anti-right

Since when can not string subscript, we put the string into digital thing.

See the specific solution to a problem that there are hundreds of dalao like it (~~ orz @ _ bright moon in half sprinkle flower %%%%%% ~ ~)

Code (single hash, 100pts):

#include <bits/stdc++.h>
#define ull unsigned long long
using namespace std;
ull base = 131;
ull a[100001];
char c[10001];
ull hashe(char s[])
{
    int l = strlen(s);
    ull ans = 0;
    for(int i = 0; i < l; i++)
    {
        ans = (ans * base + (ull)(s[i])) % 200408020617;
    }
    return ans;
}
int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> c;
        a[i] = hashe(c);
    }
    sort(a + 1, a + n + 1);
    ull ans = 1;
    for(int i = 1; i < n; i++)
    {
        if(a[i] != a[i + 1]) ans++;
    }
    cout << ans;
    return 0;
}

Note: only write the modulus is too short Gregorian birthday card? Then putGirlfriend Lunar birthday duck behind the increase in

Guess you like

Origin www.cnblogs.com/H2SO4/p/11616952.html