這個c語言問題用指標該怎麼寫?

時間 2021-05-07 06:36:13

1樓:

看到這個感覺是時候安利一波C#了,這貨名字裡面也帶著個C,讀作C Sharp。首先定義好字串:

strings1=

"There is a tree behind the house ."

;strings2=

"A big tree is cut down there ."

;接下來三步走戰略開始:

第一步,單詞拆分並去重:

var s1Words = s1.Split().Distinct();

var s2Words = s2.Split().Distinct();

第二步,求交集:

var intersect = s1Words.Intersect(s2Words);

第三步,獲取最長公共單詞:

var result = intersect.FirstOrDefault(s => s.Length == intersect.Max(s => s.Length));

當然,求交集還可以像sql一樣用自然連線實現:

varintersect

=from

w1in

s1Words

join

w2in

s2Words

onw1

equals

w2selectw1;

也可以用函式式寫法:

var intersect = s1Words.Join(s2Words, w1 => w1, w2 => w2, (w1, w2) => w1);

人生苦短,值得擁有。最後,純屬給各位推薦,不喜勿噴。

2樓:Rhythm

簡單考慮不用字典樹的話,你可以把字串切分一下,然後從大到小排個序,再遍歷一下兩個字串陣列找到第乙個相等的字串就行了,假設有N個單詞,平均長度為L,那麼這個演算法的複雜度為N * L * log(N);我簡單寫了一下:

#include

#include

#include

#define maxn 105

char

str1

[maxn

],str2

[maxn

];typedef

struct

tuple

;typedef

struct

sub_str

;int

cmp_tuple

(const

void*a

,const

void*b

)void

output_tuple

(tuplet)

putchar

('\n');}

void

gen_sub_set

(sub_str

*list

,char

*src

)qsort

(list

->pairs

,list

->count_words

,sizeof

(tuple

),cmp_tuple

);// for (int i = 0; i < list->count_words; ++i) output_tuple(list->pairs[i]);

}int

main

()output_tuple

(str1_sub_str

->pairs

[p_seg1

]);free

(str1_sub_str

);free

(str2_sub_str

);return0;

}寫成C++的話就簡單許多:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

typedef

long

longll;

typedef

unsigned

long

long

ull;

#define pipair

#define lowbit(xx)&(-(x))

#define mpmake_pair

#define irange(i, arrfor(auto&i:arr)

#define range(i, a, b) for(auto i=a;i<=b;++i)

#define itrange(i, a, b) for(auto i=a;i!=b;++i)

#define rerange(i, a, b) for(auto i=a;i>=b;--i)

#define IOS ios::sync_with_stdio(false), cin.tie(0)

#define fill(arr, tmp) memset(arr,tmp,sizeof(arr))

using

namespace

std;

/*** insert "__tmpm__".upper() then use `tmpm` command to replace it with algorithm~

** if you want to add algorithm to template, you should insert "__TMPM_START__"

* before your algorithm, and insert "__TMPM_END__" after your algorithm, then

* use `tmpm -c ` to create `template/.md` and

* insert to `template/.md` or use `tmpm -a

* ` to just insert to `template/.md`

*//// here to write const value like: const int mod = 1e9+7

/// __TMPM__

/// here to write data structure

stringx;

priority_queue

,vector

>,less

>>q1,

q2;void

init

()void

solve

()elseif(

a>b)

q1.pop();

elseq2.

pop();

}cout

<<"NONE\n"

;}intmain

(int

argc

,char

**args

)其實也可以整個AC自動機,我有現成的板子,就用C++了:(這裡不該用優先佇列的,複雜度為N*L)

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

typedef

long

longll;

typedef

unsigned

long

long

ull;

#define pipair

#define lowbit(xx)&(-(x))

#define mpmake_pair

#define irange(i, arrfor(auto&i:arr)

#define range(i, a, b) for(auto i=a;i<=b;++i)

#define itrange(i, a, b) for(auto i=a;i!=b;++i)

#define rerange(i, a, b) for(auto i=a;i>=b;--i)

#define IOS ios::sync_with_stdio(false), cin.tie(0)

#define fill(arr, tmp) memset(arr,tmp,sizeof(arr))

using

namespace

std;

/*** insert "__TEMP__" then use `tmpm` command to replace it with algorithm~

** if you want to add algorithm to template, you should insert "__TMPM_START__"

* before your algorithm, and insert "__TMPM_END__" after your algorithm, then

* use `tmpm -c ` to create `template/.md` and

* insert to `template/.md` or use `tmpm -a

* ` to just insert to `template/.md`

*//// here to write const value like: const int mod = 1e9+7

const

intmaxn

=500010+5

;const

intSIGMA_SIZE=26

;struct

ACAutomata

static

intidx

(charch)

void

init

()void

insert

(const

char

buf)

end[

now]++;

}void

build

()while(!

q.empty

())}

}int

query

(const

char

buf)

}return

res;}}

Ac;/// here to write data structure

string

str1

,str2

;priority_queue

,vector

>,less

>>q;

void

init

()void

solve

()cout

<

intmain

(int

argc

,char

**args

)AC自動機應該是最快的了;

3樓:梗中塵

既然題主已經說明是初學C語言,那我就直接用最簡單的方式寫乙個程式。

僅供參考。

#include

#include

intnumOfWords

=100

;//每個句子的最大單詞量,可以根據需要修改intmaxOfWords=45

;//每個單詞的最大字元(字母)數量,可以根據需要修改void

scan

(char

strArray

[numOfWords

][maxOfWords])}

}int

search

(char

strArray

[numOfWords

][maxOfWords

],char

*str

)int

len=

strlen

(strArray[i

]);if

(strArray[i

][len-1

]=='.')

}return0;

}int

main

()int

len=

strlen

(strArray2[i

]);if

(strArray2[i

][len-1

]=='.')}if

(maxLength!=0

)return0;}

怎麼用c語言解決這個問題

深黑灰 x 100 a x 268 b b a 168 b a b a 又因為b,a為整數,b a為168因數,所以b a小於168,所以調查168的所有因數從2到n 注 n n 168 168 b a b a 根據除數和商易求得b或a的值,於是a 100 x 劉偉奇 include include...

用 C 語言怎麼寫?

楊個毛 我的 C 語言水平,古文水平和試圖從佛經裡摳字的水平都遇到了一些瓶頸,先這樣吧。include include 型別 define 有 int define 雲 char define 空 void define 皆 const 關鍵字 define 究竟 return define 如是我...

C語言中不使用strcmp函式,怎麼用指標實現兩個字串的大小比較

這是微軟在UCRT中的實現,供參考 strcmp compare two strings,returning less than,equal to,or greater than Purpose STRCMP compares two strings and returns an integer t...