2018年9月15日 星期六

itsa [DP40-中]Find the Sequence Pattern

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<string>
using namespace std;

int main()
{
 string s1, s2, lstr, sstr;
 string s[100];
 int a, b, x;
 while (getline(cin, s1))
 {
  bool ischeck = false;
  b = 0;
  x = 0;
  getline(cin, s2);
  lstr = s1;
  sstr = s2;
  if (s1.length() < s2.length())
  {
   lstr = s2;
   sstr = s1;
  }
  for (int j = sstr.length(); j > 1; j--)
  {
   a = 0;
   if (j != b && ischeck)
    break;
   for (int i = 0; i <= sstr.length() - j; i++)
   {
    if (string::npos != lstr.find(sstr.substr(i, j)))
    {
     for (int k = 0; k < x; k++)
      if (s[k].compare(sstr.substr(i, j)) == 0)
       a = 1;
     if (a == 0)
     {
      s[x++] = sstr.substr(i, j);
      b = j;
      ischeck = true;
     }
    }
   }
  }
  if (ischeck)
  {
   if (x > 1)
   {
    for (int i = 0; i < x - 1; i++)
    {
     for (int j = i + 1; j < x; j++)
     {
      if (s[i].compare(s[j]) > 0)
      {
       string tmp = s[i];
       s[i] = s[j];
       s[j] = tmp;
      }
     }
    }
   }
   for (int i = 0; i < x; i++)
    cout << s[i] << endl;
  }
  else
   cout << "No common sequence!" << endl;
 }
 return 0;
}

itsa66 5Find the Sequence Pattern

1.找最長共同子字串,用長度小的找長度長的,長度2以上
2.最長共同子字串可以有很多個,要去掉重複的
3.輸出要按字母順序排列

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<string>
using namespace std;

int main()
{
 string s1, s2, lstr, sstr;
 string s[100];
 int a, b, x;
 while (getline(cin, s1))
 {
  bool ischeck = false;
  b = 0;
  x = 0;
  getline(cin, s2);
  lstr = s1;
  sstr = s2;
  if (s1.length() < s2.length())
  {
   lstr = s2;
   sstr = s1;
  }
  for (int j = sstr.length(); j > 1; j--)
  {
   a = 0;
   if (j != b && ischeck)
    break;
   for (int i = 0; i <= sstr.length() - j; i++)
   {
    if (string::npos != lstr.find(sstr.substr(i, j)))
    {
     for (int k = 0; k < x; k++)
      if (s[k].compare(sstr.substr(i, j)) == 0)
       a = 1;
     if (a == 0)
     {
      s[x++] = sstr.substr(i, j);
      b = j;
      ischeck = true;
     }
    }
   }
  }
  if (ischeck)
  {
   if (x > 1)
   {
    for (int i = 0; i < x - 1; i++)
    {
     for (int j = i + 1; j < x; j++)
     {
      if (s[i].compare(s[j]) > 0)
      {
       string tmp = s[i];
       s[i] = s[j];
       s[j] = tmp;
      }
     }
    }
   }
   for (int i = 0; i < x; i++)
    cout << s[i] << endl;
  }
  else
   cout << "No common sequence!" << endl;
 }
 return 0;
}

2018年9月13日 星期四

itsa [C_ST125-易]字串切割

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<string>
#include<sstream>
using namespace std;

void replaceD(string &s)
{
 int i = 0;
 while (i < s.length())
 {
  if (s[i] == ':' || s[i] == ',' || s[i] == ';')
   s[i] = ' ';
  i++;
 }
}

int main()
{
 int N, x = 0;
 string s, str[100];
 cin >> N;
 getline(cin, s);
 for (int i = 0; i < N; i++)
 {
  x = 0;
  getline(cin, s);
  cout << "Original string: " << s << endl;
  replaceD(s);
  stringstream ss(s);
  while (ss >> str[x])
   x++;
  cout << "Tokens found:\n";
  for (int j = 0; j < x; j++)
   cout << str[j] << endl;
 }
 return 0;
}

itsa [C_MM359-易]未知數

#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iomanip>
using namespace std;

int main()
{
double ans;
string s1, s2, s3, s4, s5;
while (cin >> s1 >> s2 >> s3 >> s4 >> s5)
{
ans = 0;
if (s1 == "x")
{
if (s2 == "+")
ans = atof(s5.c_str()) - atof(s3.c_str());
else if (s2 == "-")
ans = atof(s5.c_str()) + atof(s3.c_str());
else if (s2 == "*")
ans = atof(s5.c_str()) / atof(s3.c_str());
else
ans = atof(s5.c_str()) * atof(s3.c_str());
}
else if (s3 == "x")
{
if (s2 == "+")
ans = atof(s5.c_str()) - atof(s1.c_str());
else if (s2 == "-")
ans = atof(s1.c_str()) - atof(s5.c_str());
else if (s2 == "*")
ans = atof(s5.c_str()) / atof(s1.c_str());
else
ans = atof(s1.c_str()) / atof(s5.c_str());
}
else
{
if (s2 == "+")
ans = atof(s1.c_str()) + atof(s3.c_str());
else if (s2 == "-")
ans = atof(s1.c_str()) - atof(s3.c_str());
else if (s2 == "*")
ans = atof(s1.c_str()) * atof(s3.c_str());
else
ans = atof(s1.c_str()) / atof(s3.c_str());
}
ans = floor(ans * 10) / 10;
cout << fixed << setprecision(1) << ans << endl;
}
return 0;
}

itsa [C_AR69-中]羅馬數字系統

#include<iostream>
#include<string>
using namespace std;

int main()
{
string one[4] = { "I","X","C","M" };
string five[3] = { "V","L","D" };
string s;
int n;
cin >> n;
getline(cin, s);
for (int k = 0; k < n; k++)
{
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
int subNum = s[i] - '0';
int num = 0;
if (subNum < 5)
num = subNum % 4;
else if (subNum > 5)
num = (subNum - 5) % 4;
if (subNum >= 1 && subNum <= 4)
{
if (num == 0)
{
cout << one[s.length() - i - 1];
cout << five[s.length() - i - 1];
}
else
{
for (int j = 0; j < num; j++)
cout << one[s.length() - i - 1];
}
}
else if (subNum == 5)
cout << five[s.length() - i - 1];
else if (subNum >= 6 && subNum <= 9)
{
if (num == 0)
{
cout << one[s.length() - i - 1];
cout << one[s.length() - i];
}
else
{
cout << five[s.length() - i - 1];
for (int j = 0; j < num; j++)
cout << one[s.length() - i - 1];
}
}
}
cout << endl;
}
return 0;
}

itsa66 4羅馬數字系統

1.將羅馬數字存到陣列,區分五跟十的倍數。
2.以五為分界,前四做前輸出,後四做後輸出。
3.以迴圈做對應輸出。
注意:陣列存的方式會改變輸出時的陣列編號。

#include<iostream>
#include<string>
using namespace std;

int main()
{
string one[4] = { "I","X","C","M" };
string five[3] = { "V","L","D" };
string s;
int n;
cin >> n;
getline(cin, s);
for (int k = 0; k < n; k++)
{
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
int subNum = s[i] - '0';
int num = 0;
if (subNum < 5)
num = subNum % 4;
else if (subNum > 5)
num = (subNum - 5) % 4;
if (subNum >= 1 && subNum <= 4)
{
if (num == 0)
{
cout << one[s.length() - i - 1];
cout << five[s.length() - i - 1];
}
else
{
for (int j = 0; j < num; j++)
cout << one[s.length() - i - 1];
}
}
else if (subNum == 5)
cout << five[s.length() - i - 1];
else if (subNum >= 6 && subNum <= 9)
{
if (num == 0)
{
cout << one[s.length() - i - 1];
cout << one[s.length() - i];
}
else
{
cout << five[s.length() - i - 1];
for (int j = 0; j < num; j++)
cout << one[s.length() - i - 1];
}
}
}
cout << endl;
}
return 0;
}

itsa66 3字串切割

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

void replaceD(string &s)
{
int i = 0;
while (i < s.length())
{
if (s[i] == ':' || s[i] == ',' || s[i] == ';')
s[i] = ' ';
i++;
}
}

int main()
{
int N, x = 0;
string s, str[100];
cin >> N;
getline(cin, s);
for (int i = 0; i < N; i++)
{
x = 0;
getline(cin, s);
replaceD(s);
stringstream ss(s);
while (ss >> str[x])
x++;
cout << "Tokens found:\n";
for (int j = 0; j < x; j++)
cout << str[j] << endl;
}
return 0;
}

itsa66 2未知數

1.無條件捨去,引用<cmath>的floor。
2.字串轉整數或小數,要先轉字元, "c_str()"可以將字串轉字元,但要看情況使用。
例:1111 2222 333
s[   0     1    2  ]
s[0][1] = 1  這時有[][]就不能用s[0][1].c_str() 要不然會錯喔!
注意:可以為小數,運算要正確。

#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iomanip>
using namespace std;

int main()
{
double ans;
string s1, s2, s3, s4, s5;
while (cin >> s1 >> s2 >> s3 >> s4 >> s5)
{
ans = 0;
if (s1 == "x")
{
if (s2 == "+")
ans = atof(s5.c_str()) - atof(s3.c_str());
else if (s2 == "-")
ans = atof(s5.c_str()) + atof(s3.c_str());
else if (s2 == "*")
ans = atof(s5.c_str()) / atof(s3.c_str());
else
ans = atof(s5.c_str()) * atof(s3.c_str());
}
else if (s3 == "x")
{
if (s2 == "+")
ans = atof(s5.c_str()) - atof(s1.c_str());
else if (s2 == "-")
ans = atof(s1.c_str()) - atof(s5.c_str());
else if (s2 == "*")
ans = atof(s5.c_str()) / atof(s1.c_str());
else
ans = atof(s1.c_str()) / atof(s5.c_str());
}
else
{
if (s2 == "+")
ans = atof(s1.c_str()) + atof(s3.c_str());
else if (s2 == "-")
ans = atof(s1.c_str()) - atof(s3.c_str());
else if (s2 == "*")
ans = atof(s1.c_str()) * atof(s3.c_str());
else
ans = atof(s1.c_str()) / atof(s3.c_str());
}
ans = floor(ans * 10) / 10;
cout << fixed << setprecision(1) << ans << endl;
}
return 0;
}

itsa66 1過半元素

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
int arr[30];
int N, count, a;
string str;
while (getline(cin, str))
{
a = 0;
N = 0;
stringstream ss(str);
while (ss >> arr[N])
N++;
N--;
for (int i = 0; i < N; i++)
{
count = 0;
for (int j = 0; j < N; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count > N / 2)
{
a = 1;
cout << arr[i] << endl;
break;
}
}
if (count <= N / 2 && a == 0)
cout << "NO" << endl;
}
return 0;
}

2018年9月8日 星期六

itsa [C_AR030-中]文字頻率分析

註: tUp英文大寫次數 tLow英文小寫次數 tw字次數

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
int c, w, count = 0, x = 0, y = 0, z = 0, g = 0, a = 0, b = 0, check = 0;
int tUp[26] = { 0 }, tLow[26] = { 0 }, tw[100] = { 0 };
string prefix, suffix, s, str[100], pre[100], suf[100], wo[100];
cin >> prefix;
cin >> suffix;
cin >> c;
cin >> w;
getline(cin, s);
getline(cin, s);
stringstream ss(s);
while (ss >> str[x])
x++;
for (int i = 0; i < x; i++)
{
check = 0;
count = 0;
if (str[i].length() >= prefix.length())
{
for (int j = 0; j < prefix.length(); j++)
{
if (str[i][j] == prefix[j])
count++;
else
break;
}
if (count == prefix.length())
{
for (int k = 0; k < y; k++)
if (pre[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
pre[y++] = str[i];
}
}
}
for (int i = 0; i < x; i++)
{
a = 0;
count = 0;
check = 0;
if (str[i].length() >= suffix.length())
{
for (int j = str[i].length() - suffix.length(); j < str[i].length(); j++)
{
if (str[i][j] == suffix[a++])
count++;
else
break;
}
if (count == suffix.length())
{
for (int k = 0; k < z; k++)
if (suf[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
suf[z++] = str[i];
}
}
}
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < s.length(); j++)
{
if (s[i] == s[j])
{
if (s[i] >= 97 && s[i] <= 122)
tLow[s[i] - 'a']++;
else if (s[i] >= 65 && s[i] <= 90)
tUp[s[i] - 'A']++;
s[j] = ' ';
}
}
}
for (int i = 0; i < x; i++)
{
b = 0;
check = 0;
for (int j = 0; j < x; j++)
{
count = 0;
if (str[i].length() == str[j].length())
{
for (int k = 0; k < str[i].length(); k++)
{
if (str[i][k] == str[j][k])
count++;
else
break;
}
}
if (count == str[i].length())
b++;
}
if (b >= w)
{
for (int k = 0; k < g; k++)
if (wo[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
{
wo[g] = str[i];
tw[g] = b;
g++;
}
}
}
for (int i = 0; i < g - 1; i++)
{
for (int j = i + 1; j < g; j++)
{
if (wo[i].compare(wo[j]) == 1)
{
string tmp = wo[i];
wo[i] = wo[j];
wo[j] = tmp;
int temp = tw[i];
tw[i] = tw[j];
tw[j] = temp;
}
}
}
cout << "prefix of " << prefix << ":" << endl;
for (int i = 0; i < y; i++)
cout << pre[i] << endl;
cout << "suffix of " << suffix << ":" << endl;
for (int i = 0; i < z; i++)
cout << suf[i] << endl;
cout << "character frequency over " << c << ":" << endl;
for (int i = 0; i < 26; i++)
{
if (tUp[i] >= c)
cout << (char)(i + 'A') << ',' << tUp[i] << endl;
}
for (int i = 0; i < 26; i++)
{
if (tLow[i] >= c)
cout << (char)(i + 'a') << ',' << tLow[i] << endl;
}
cout << "word frequency over " << w << ":" << endl;
for (int i = 0; i < g; i++)
cout << wo[i] << ',' << tw[i] << endl;
return 0;
}

2018年9月2日 星期日

itsa [C_AR029-難]文字編碼

#include<iostream>
#include<string>
using namespace std;

int main()
{
int m = 0, x = 0, check = 0;
char arr[16][16];
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
arr[i][j] = ' ';
string s;
getline(cin, s);
for (int i = 1; i * i <= s.length(); i++)
m = i;
if (m * m < s.length())
m++;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
arr[i][j] = s[x++];
if (x == s.length())
break;
}
if (x == s.length())
break;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
cout << arr[j][i];
cout << endl;
return 0;
}